PowerShell offers a variety of math functions you can use in your scripts. In this post, I introduce PowerShell's basic math functions.

Baki Onur Okutucu

Onur is a subject matter expert for Office 365, Azure, and PowerShell technologies. He is the founder of Clouderz Ltd, a cloud consultancy based in London. For eight years in a row, Microsoft has recognized him as a Most Valuable Professional. You can follow Onur on Twitter: @BakiOnur.

Basic calculations ^

It is convenient using PowerShell for your everyday calculation tasks. You don't need any variables to play with four basic math operations. Just put in the numbers and let PowerShell do the math!

Four operations with PowerShell

Four operations with PowerShell

If you wish, you can also use variables, which is much easier when it comes to multiple complex operations. PowerShell supports a number of variable types such as String, DateTime, and Integer. Whenever you put a number in a variable, it automatically becomes an integer variable.

PowerShell converts numbers to integers

PowerShell converts numbers to integers

You can then apply the basic math operations with the same syntax you learned in school.

Using variables for calculations

Using variables for calculations

Obviously, PowerShell does not only provide the four basic operations, but it also provides a wide range of functions you can use in various operations.

PowerShell math library ^

PowerShell uses the [System.Math] library, which consists of many mathematical functions and methods. You can list all methods this library offers with the following commands:

PowerShell [System.Math] library

PowerShell [System.Math] library

There are also two static properties, which are pi and E (epsilon) constants. The syntax to access the value of these constants is quite straightforward:

pi

e

Pi and Epsilon constants

Pi and Epsilon constants

Important PowerShell math methods ^

Let's see the other useful functions that can make your life easier when doing math in PowerShell.

Round

Use this for rounding off numbers to the nearest decimal value.

Round function

Round function

Square root

 To calculate the square root of a number, you can use this command:

Square root function

Square root function

Powers

If you have to calculate the power of a base number with a certain exponent, you can use the next command. In my example, 6 is the base and 2 the exponent.

Powers function

Powers function

Absolute value

 The absolute value of a number is its non-negative value without regard to its sign.

Absolute value function

Absolute value function

Trigonometric functions

Trigonometric functions apply to calculations related to angles. I suppose you remember them well from school.

Sine

Cosine

Tangent

Trigonometric functions

Trigonometric functions

Logarithm

Logarithm is a function used to reverse the operation of exponentiation. For example, when the third power of 10 equals 1000, the logarithm of 1000 with respect to base 10 is 3.

Logarithm function

Logarithm function

Data units

PowerShell allows you to convert the size of data units (KB, MB, GB, TB, and PB). The syntax is quite straightforward:, 1KB, 1MB, 1GB, 1TB, 1PB.

Size of data units

Size of data units

A math example ^

Let's calculate the area of a circle using PowerShell. This is the formula to calculate the area:

A=πr2

The area of a circle

The area of a circle

In PowerShell, the calculation looks like this:

Calculating the area of a circle in PowerShell

Calculating the area of a circle in PowerShell

In the example, the radius of the area is 2.

Win the monthly 4sysops member prize for IT pros

Share
2+

Users who have LIKED this post:

  • avatar

Related Posts

0 Comments

Leave a reply

Your email address will not be published. Required fields are marked *

*

CONTACT US

Please ask IT administration questions in the forum. Any other messages are welcome.

Sending
© 4sysops 2006 - 2017

Log in with your credentials

or    

Forgot your details?

Create Account