### Baki Onur Okutucu

#### Latest posts by Baki Onur Okutucu (see all)

- PowerShell script for creating a new Azure VM using a disk snapshot - Fri, Dec 8 2017
- Do the math with PowerShell - Thu, Nov 30 2017
- Manage Azure role-based access control with PowerShell - Tue, Nov 14 2017

## 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!

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.

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

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:

1 2 | [System.Math] | Get-Member -Static -MemberType All [Math] |Get-Member -Static -MemberType All |

**pi**

1 | [Math]::pi |

**e**

1 | [Math]::e |

## 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.

1 | [Math]::Round |

### Square root

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

1 | [Math]::Sqrt(64) |

### 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.

1 | [Math]::Pow(6,2) |

### Absolute value

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

1 | [Math]::Abs(-5) |

### Trigonometric functions

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

*Sine*

1 | [Math]::Sin(90) |

*Cosine*

1 | [Math]::Cos(90) |

*Tangent*

1 | [Math]::Tan(30) |

### 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.

1 | [Math]::Log10(1000) |

### 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.

## A math example ^

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

A=πr2

In PowerShell, the calculation looks like this:

1 2 3 4 5 | $r=2 $pi=[math]::pi $r2=[math]::pow( $r, 2 ) $Area = $pi*$r2 $Area |

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

**Win the monthly 4sysops member prize for IT pros**