Guy recommends :
Free Permissions
Analyzer Tool

Solarwinds Free Download of Permissions Analyzer

 

View the effective permissions for a folder or shared drive. Free download try it now!


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] PowerShell's double colon (::) introduces the function, in this case Round.

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

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, as in two decimal places.

Note 7:  Check the logic of PowerShell's brackets.

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
}

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:  A Free Trial of the Network Performance Monitor (NPM)Review of Orion NPM v10

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

Researching Static Properties

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

[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)
GetProcesses Method static System.Diagnostics.Process[] GetProcesses(), static System.Diagnostics.Process[] GetProcesses(string machineName)
GetProcessesByName Method static System.Diagnostics.Process[] GetProcessesByName(string processName), static System.Diagnostics.Process[] GetProcessesByName(string processName, string machineName)
LeaveDebugMode Method static void LeaveDebugMode()
ReferenceEquals Method static bool ReferenceEquals(System.Object objA, System.Object objB)
Start Method static System.Diagnostics.Process Start(string fileName, string userName, securestring password, string domain), static System.Diagnostics.Process Start(string fileName, s...

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 the System.Math. PowerShell has no separate math functions.  There is no need 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.

Download my ebook:Getting Started with PowerShell
Getting Started with PowerShell - only $9.25

You get 36 topics organized into these 3 sections:
   1) Getting Started
   2) Real-life tasks
   3) Examples of Syntax.

In addition to the ebook, you get a PDF version of this  Introduction to PowerShell ebook  It runs to 120 pages of A4.

 *


Custom Search

Site Home

Guy Recommends: WMI Monitor for PowershellSolarwinds WMI Monitor

Windows Management Instrumentation (WMI) is most useful for PowerShell scripting.

SolarWinds have produced this Free WMI Monitor to take the guess work out of which WMI counters to use for applications like Microsoft Active Directory, SQL or Exchange Server.

Download your free copy of WMI Monitor

Author: Guy Thomas Copyright © 1999-2013 Computer Performance LTD All rights reserved.

Please report a broken link, or an error to: