Guy recommends :
Free Solarwinds
VM Console

Solarwinds VM Console Free Download

Find out which of your VMs are a waste of space and which VMs need more resources.



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.  Have you 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

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]

Guy Recommends:  Solarwinds' Free Bulk Import ToolFree Download of Solarwinds  Bulk Import Tool

Import users from a spreadsheet.  Just provide a list of the users with their fields in the top row, and save as .csv file.  Then launch this FREE utility and match your fields with AD's attributes, click to import the users.  Optionally, you can provide the name of the OU where the new accounts will be born.

There are also two bonus tools in this free download, and all 3 have been approved by Microsoft:

  1. Bulk-import new users into Active Directory.
  2. Seek and zap unwanted user accounts.
  3. Find inactive computers.

Download your FREE bulk import tool.

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.

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:  Other cmdlets have static properties for example,

Get-Process | Get-Member -Static

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 Microsoft PowerShell tutorials

PowerShell Tutorials  • Methods  • Cmdlets  • PS Snapin  • Profile.ps1  • Exchange 2007

Command & Expression Mode  • PowerShell pipeline (|)  • PowerShell 'where'  • PowerShell 'Sort'

If you see an error of any kind, do let me know.  Please report any factual mistakes, grammatical errors or broken links, I will be happy to not only to correct the fault, but also to give you credit.

 *


Custom Search

Guy Recommends: WMI Monitor and It's Free!Solarwinds WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft operating systems.

Fortunately, Solarwinds have created the Free WMI Monitor so that you can actually see and understand these gems of performance information.  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

 

Home Copyright © 1999-2012 Computer Performance LTD All rights reserved

Please report a broken link, or an error.