Windows PowerShell Math Functions

Windows PowerShell System.Math::Static Method

There are no separate math functions in PowerShell.  So when you need to access a maths function such as Round or Sqrt, just call for the System.Math.  If you have ever wondered about the PowerShell double colon (::), then all is revealed, it means a static method.

 ♣

PowerShell System.Math Round

Once you call for [System.Math] the double colon (::) introduces the function, in this case Round.  I think of rounding as a method, hence PowerShell uses the (parenthesis) brackets for the number to be processed.

# PowerShell System.Math Round static method
Clear-Host
$Num = 49.6814343
[System.Math]::Round($Num, 2)

Note 1: This example rounds to two decimal places.

Note 2: You could shorten [System.Math] to [Math].

PowerShell System.Math Truncate

Rather than rounding, you could achieve much the same result with truncate.

# PowerShell System.Math truncate method
Clear-Host
$Num = 49.6814343
[Math]::Truncate($Num)

Note 3: As an alternative I have used [Math] instead of [System.Math]; the pair seem interchangeable.

Note 4: Incidentally, ‘Round’ calculates to the nearest number, which means it will round up.  Whereas Truncate just chops off the decimal places.  Thus:

Round $Num = 49.6814343.  The result is: 50
Truncate $Num = 49.6814343.  The result is: 49

Guy Recommends:  A Free Trial of the Network Performance Monitor (NPM)Review of Orion NPM v11.5 v11.5

SolarWinds’ Network Performance Monitor will help you discover what’s happening on your network.  This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.

What I like best is the way NPM suggests solutions to network problems.  Its also has the ability to monitor the health of individual VMware virtual machines.  If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.

Download a free trial of Solarwinds’ Network Performance Monitor

PowerShell System.Math Square Root

# PowerShell System.Math::static method Sqrt (Square Root)
Clear-Host
$Num = 49
[System.Math]::Sqrt($Num)

Note 5: Observe that there is no need to create an object for the Math class, we simply employ :: before the method().

Square Root and Round, 2 Places

# PowerShell Math static method ::Sqrt (Square Root)
Clear-Host
$Num = 490
[Math]::Round([Math]::Sqrt($Num),2)

Note 6: Observe the position of the 2, it's purpose is to prescribe two decimal places.

Note 7: Check the logic of PowerShell's brackets in relation to the methods 'Round' and 'Squrt'.

Note 8: Here [System.Math] has been abbreviated to [Math]

PowerShell System.Math in Action

Here is an example which works with % CPU time, what System.Math::Round does is remove those confusing decimal places in the CPU value.

# PowerShell Math static method ::Round
Clear-Host
$Proc = Get-Process | Sort-Object cpu -desc
ForEach ($Item in $Proc) {
$CPU =[System.Math]::Round($Item.CPU)
"{0,-28} {1,10} {2,20}" -f `
$Item.name, $CPU, $Item.WorkingSet
}

A Superior Method -AS

If you just want to get the job done (rounding CPU), rather than testing System.Math, then this may be a simpler technique.

Clear-Host
$CPU_Calc = @{ Label = "CPU Time"; Expression={($_.cpu) -as [int] }}
Get-Process | Sort-Object cpu -descending |
Format-Table Name, $CPU_Calc, WorkingSet -auto

Note 9: The rounding or truncating is controlled by @{Label = ; Expression=}; observe the -as [int] construction.  See more about the PowerShell -AS operator.

Guy Recommends: Free WMI Monitor for PowerShellSolarwinds Free WMI Monitor for PowerShell

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft’s operating systems.  Fortunately, SolarWinds have created a Free WMI Monitor so that you can discover these gems of performance information, and thus improve your PowerShell scripts.

Take the guess work out of which WMI counters to use when scripting the operating system, Active Directory, or Exchange Server. Give this WMI monitor a try – it’s free.

Download your free copy of WMI Monitor

Researching Static Properties

The secret of finding more static properties is employ Get-Member with the -static parameter.

[Math] | Get-Member -Static

TypeName: System.Math

Name    MemberType Definition 
----- ---------- -------------------------------
Abs Method static sbyte Abs(sbyte value), static float
Acos Method static double Acos(double d)
Asin Method static double Asin(double d)
Atan Method static double Atan(double d)
Atan2 Method static double Atan2(double y, double x)
BigMul Method static long BigMul(int a, int b)
Ceiling Method static decimal Ceiling(decimal d
Cos Method static double Cos(double d)
Cosh Method static double Cosh(double value)
DivRem Method static int DivRem(int a, int b, [ref] int result)
Equals Method static bool Equals(System.Object objA, objB)
Exp Method static double Exp(double d)
Floor Method static decimal Floor(decimal d) IEEERemainder Method static double IEEERemainder(double x, double y)
Log Method static double Log(double d)
Log10 Method static double Log10(double d)
Max Method static sbyte Max sbyte val1, sbyte val2) Min Method static sbyte Min(sbyte val1, sbyte val2)
Pow Method static double Pow(double x, double y)
Round Method static double Round(double a), static double
Sinh Method static double Sinh(double value)
Sqrt Method static double Sqrt(double d)
Tan Method static double Tan(double a)
Tanh Method static double Tanh(double value)
Truncate Method static decimal Truncate(decimal d)
E Property static double E {get;}
PI Property static double PI {get;}

More Research

[System.IO.File] | Get-Member -Static

Note 10: Try substituting other objects for .File, for example, Path or DriveInfo.

Note 11: Most other cmdlets have at least one static properties for example,

Get-Process | Get-Member -Static

TypeName: System.Diagnostics.Process

Name MemberType Definition
—- ———- ———-
EnterDebugMode    Method static void EnterDebugMode()
Equals                     Method static bool Equals(System.Object objA, System.Object objB)
GetCurrentProcess  Method static System.Diagnostics.Process GetCurrentProcess()
GetProcessById      Method static System.Diagnostics.Process GetProcessById(int processId, string machineName), static System.Diagnostics.Process GetProcessById(int processId)
……………………

See more on PowerShell Math »

Summary of PowerShell Math Functions

When you need to access a maths function such as Round or Truncate, just call for System.Math; PowerShell has no separate math functions.  You don't have to create an object for the Math class, simply employ a double colon (::) before the method.

If you like this page then please share it with your friends

 


See more PowerShell examples of math operations

PowerShell Maths Index   • Windows PowerShell   • PowerShell Splatting

PowerShell Math  • Comparison Operators   • PowerShell Measure Object   • Measure Command

PowerShell Tutorials  • Syntax  • Pipeline  • PowrShell Quotes  • Free WMI Monitor

Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.