PowerShell supports mathematical tasks primarily through its support for the System.Math class in the .NET Framework.
To explore what methods we have in Math class, lets run this command:
[Math] | Get-Member -Static -MemberType Method
TypeName | Name | MemberType | Definition |
System.Math | Abs | Method | static sbyte Abs(sbyte value),
static int16 Abs(int16 value), static int Abs(int value), static long Abs(long value),
static float Abs(float value), static double Abs(double value), static decimal Abs(de
cimal value) |
System.Math | Acos | Method | static double Acos(double d)</
td> |
System.Math | Asin | Method | static double Asin(double d)</
td> |
System.Math | Atan | Method | static double Atan(double d)</
td> |
System.Math | Atan2 | Method | static double Atan2(double y,
double x) |
System.Math | BigMul | Method | static long BigMul(int a, in
t b) |
System.Math | Ceiling | Method | static decimal Ceiling(deci
mal d), static double Ceiling(double a) |
System.Math | Cos | Method | static double Cos(double d) |
System.Math | Cosh | Method | static double Cosh(double valu
e) |
System.Math | DivRem | Method | static int DivRem(int a, int
b, [ref] int result), static long DivRem(long a, long b, [ref] long result) |
System.Math | Equals | Method | static bool Equals(System.Ob
ject objA, System.Object objB) |
System.Math | Exp | Method | static double Exp(double d) |
System.Math | Floor | Method | static decimal Floor(decimal
d), static double Floor(double d) |
System.Math | IEEERemainder | Method | static double IEEERem
ainder(double x, double y) |
System.Math | Log | Method | static double Log(double d), st
atic double Log(double a, double newBase) |
System.Math | Log10 | Method | static double Log10(double d)
|
System.Math | Max | Method | static sbyte Max(sbyte val1, sb
yte val2), static byte Max(byte val1, byte val2), static int16 Max(int16 val1, int16 v
al2), static uint16 Max(uint16 val1, uint16 val2), static int Max(int val1, int val2),
static uint32 Max(uint32 val1, uint32 val2), static long Max(long val1, long val2), s
tatic uint64 Max(uint64 val1, uint64 val2), static float Max(float val1, float val2),
static double Max(double val1, double val2), static decimal Max(decimal val1, decimal
val2) |
System.Math | Min | Method | static sbyte Min(sbyte val1, sb
yte val2), static byte Min(byte val1, byte val2), static int16 Min(int16 val1, int16 v
al2), static uint16 Min(uint16 val1, uint16 val2), static int Min(int val1, int val2),
static uint32 Min(uint32 val1, uint32 val2), static long Min(long val1, long val2), s
tatic uint64 Min(uint64 val1, uint64 val2), static float Min(float val1, float val2),
static double Min(double val1, double val2), static decimal Min(decimal val1, decimal
val2) |
System.Math | Pow | Method | static double Pow(double x, dou
ble y) |
System.Math | ReferenceEquals | Method | static bool Referen
ceEquals(System.Object objA, System.Object objB) |
System.Math | Round | Method | static double Round(double a)
, static double Round(double value, int digits), static double Round(double value, Sys
tem.MidpointRounding mode), static double Round(double value, int digits, System.Midpo
intRounding mode), static decimal Round(decimal d), static decimal Round(decimal d, in
t decimals), static decimal Round(decimal d, System.MidpointRounding mode), static dec
imal Round(decimal d, int decimals, System.MidpointRounding mode) |
System.Math | Sign | Method | static int Sign(sbyte value),
static int Sign(int16 value), static int Sign(int value), static int Sign(long value),
static int Sign(float value), static int Sign(double value), static int Sign(decimal
value) |
System.Math | Sin | Method | static double Sin(double a) |
System.Math | Sinh | Method | static double Sinh(double valu
e) |
System.Math | Sqrt | Method | static double Sqrt(double d)</
td> |
System.Math | Tan | Method | static double Tan(double a) |
System.Math | Tanh | Method | static double Tanh(double valu
e) |
System.Math | Truncate | Method | static decimal Truncate(de
cimal d), static double Truncate(double d) |
And also get properties:
[Math] | Get-Member -Static -MemberType Property
TypeName: System.Math
Name MemberType Definition
---- ---------- ----------
E Property static double E {get;}
PI Property static double PI {get;}
These methods are as simple to use as it seems. Lets test few methods.
function trunc($number) { [Math]::Truncate($number) }
$result = 3/2
trunc $result
1
Math.Truncate method will converts the decimal number to integral value. We have placed this method in a script block to define our own PowerShell function and then we called it in PowerShell way.
For absolute value of a number, [Math]::Abs() method:
[Math]::Abs(-10.6)
10.6
To find the power of a number, use the [Math]::Pow() method.
[Math]::Pow(5, 2)
25
For Square root of a number, use the [Math]::Sqrt() method:
[Math]::Sqrt(100)
10
To find the sine, cosine, or tangent of an angle (given in radians), use the [Math]::Sin(),
[Math]::Cos(), or [Math]::Tan() method:
[Math]::Sin( [Math]::PI / 2 )
1
Similarly to find the angle of a sine, cosine, or tangent value, use the [Math]::ASin(), [Math]::ACos(), or [Math]::ATan() methods
Use the Measure-Object cmdlet to measure these statistical properties of a list. Just pipe the objects to the Measure-Object cmdlet:
1..10 | Measure-Object -Average -Sum -Maximum -Minimum
Count : 10
Average : 5.5
Sum : 55
Maximum : 10
Minimum : 1
Property :
To measure the numeric features of a specific property in a stream of objects, supply that property name to the -Property parameter of the Measure-Object cmdlet. For example, in a directory with files:
Get-ChildItem | Measure-Object -Property Length -Max -Min -Average -Sum
Count : 16
Average : 831.25
Sum : 13300
Maximum : 2756
Minimum : 67
Property : Length
References:
Windows PowerShell Cookbook