How-to: PowerShell Math operators

Basic arithmetic operators:

Addition (+) - Add numbers, concatenate strings, arrays, and hash tables.

  6 + 2                        # result = 8
  "file" + "name"              # result = "filename"
  @(1, "one") + @(2.0, "two")  # result = @(1, "one", 2.0, "two")
  @{"one" = 1} + @{"two" = 2}  # result = @{"one" = 1; "two" = 2}

Subtraction (-) - Subtract or negate numbers.

  6 - 2   # result = 4
  - -6    # result = 6
  (Get-Date).AddDays(-1) # Yesterday’s date

Multiplication (*) - Multiply numbers or copy strings and arrays the specified number of times.

  6 * 2       # result = 12
  @("!") * 4  # result = @("!","!","!","!")
  "!" * 3     # result = "!!!"

Division (/) - Divide numbers, When the quotient of a division operation is an integer, PowerShell will round the value to the nearest integer. When the value is .5, it will round to the nearest even integer.

  6 / 2  # result = 3  

Modulus (%) - Return the remainder of a division operation.

  7 % 2  # result = 1

.Net Math functions

In addition to standard PowerShell string methods we can also call .Net Math functions:

 .NET Math library:

  [math]::Round($var, p)  Round a variable to p places.
  [math]::Round($var)
  [math]::Abs(n)
  [math]::BigMul
  [math]::Ceiling(n)   Rounds everything up, towards positive infinity.
  [math]::DivRem
  [math]::Equals(n,m)  Compare two values and return a [boolean] (true/false) result. (unlike -eq the types must also match)
  [math]::Exp(p)       Raise e to a power p
  [math]::Floor(n)     Rounds everything down, towards negative infinity.
  [math]::GetHashCode
  [math]::GetType
  [math]::Log(n)    base e logarithm of n
  [math]::Log10(n)  base 10 logarithm of n
  [math]::Max(n,m)  Return the larger of two values
  [math]::Min(n,m)  Return the smaller of two values
  [math]::Pow(n,p)  Raise a number n to a power p
  [math]::IEEERemainder
  [math]::Round(n,p) Round a number n to p decimal places. Default=0 decimal places.
  [math]::Sign
  [math]::Sqrt(n)   Square Root of a number n
  [math]::ToString
  [math]::Truncate(n)  Remove the decimal returning only an Integer. Rounds toward zero.

The trig functions all accept an angle, measured in radians.
To convert degrees to radians multiply by pi/180 using [Math]::PI 

  [math]::Acos
  [math]::Asin
  [math]::Atan
  [math]::Atan2
  [math]::Cos
  [math]::Cosh
  [math]::Sin
  [math]::Tan
  [math]::Sinh
  [math]::Tanh

Static mathematical constants:
  [math]::e
  [math]::pi

You can list all the static memners of system.math like this: (via /\/\o\/\/ )

 [system.math] | gm -static

Examples

Find the Minimum value of two variables:

$var1 = 764.4
$var2 = 1409
$answer = [math]::min($var1,$var2)
$answer

Find the Tangent of 45 degrees:

$answer = [System.Math]::Tan(45/180*[System.Math]::PI)
$answer

“Film is one of the three universal languages, the other two: mathematics and music” ~ Frank Capra

Related PowerShell Cmdlets

math.class - MSDN scroll down for full definitions.
Concatenation - Several methods to combine strings together.
PowerShell Operators - More advanced Operators for Arrays and formatting expressions.


 
Copyright © 1999-2024 SS64.com
Some rights reserved