Windows PowerShell -As Type Operator

Windows PowerShell -As Type Operator

My main use for PowerShell’s -as operator is to truncate decimals, for example:

# PowerShell -As Type Operator
341.1134143891 -as [int]
# Produces the answer
# 341

Note 1: Naturally, this operator is not case sensitive, you can use -as, -As or even -AS.

Topics for PowerShell’s -As Operator Type

 ♣

PowerShell -As Operator with Get-Process

What’s happening is that PowerShell uses an old C# programming trick and uses -as to modify the underlying .Net Framework Type.

In this example the problem was that PowerShell displays the CPU usage to six places of decimal – totally unnecessary and possibly confusing.

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

Note 2: The key is adding @{Label = ; Expression =}, then using -as to control the decimals.  To see what I mean substitute ‘-as [decimal]’ for ‘-as[int]’.

Note 3: I chose to employ the variable $CPU to highlight how Format-Table displays the value in the shortened form.

Note 4: Remember that -as accesses the .Net Framework types.

PowerShell -As Example with Measure-Object

Scenario:  You want to measure the response time to a website, you would like the result as a whole number, and wish to truncate the string of confusing decimals.

# PowerShell -As simple example
Write-Host `n "Waiting for test …"
$Avg = 0
$Site = "www.google.com"
$PingSite = Test-Connection -count 5 $Site
$Avg = ($PingSite | Measure-Object ResponseTime -average)
$Calc = ($Avg.average) -as [int]
Clear-host
Write-Host "Average response time to $Site is $Calc ms"

Note 5: To see the contrast try substituting -as [Decimal] for -as [int].

Note 6: One area where -as is useful is with WMI classes.

Note 7: See more on PowerShell’s Measure-Object

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 -As Compared with System.Math

As an alternative to -as you could employ System.Math.  Here is System.Math calculating a square root.  The problem is we don’t need that number of decimals.

# PowerShell Math Round
Clear-Host
$Num = 500
"Square root of $Num is " + [System.Math]::Round([System.Math]::Sqrt($Num))

Solution using System.Math itself

# PowerShell Math Round
Clear-Host
$Num = 500
[System.Math]::Round([System.Math]::Sqrt($Num))

Solution using -as

# PowerShell Math Round
Clear-Host
$Num = 500
$Sqrt = [System.Math]::Sqrt($Num) -as [Int]
"Square root of $Num is approx: " + $Sqrt

One benefit of using System.Math’s Round is that you could control the number of decimal places.  However, my overall point is that with PowerShell, as with the whole of Microsoft, there are always multiple ways of achieving the same result.

Researching -As Type Properties

The secret of finding more information is to call for Get-Help About … Operators

Clear-Host
Get-Help about_*_operators

Name
—- ——– ——–
about_Arithmetic_Operators
about_Assignment_Operators
about_Comparison_Operators
about_logical_operators
about_type_operators

Clear-Host
Get-Help about_type_operators

Get more help on PowerShell’s About_ files »

Summary of PowerShell -As

-As converts the input to the specified .NET Framework type.  The best example of defining the operator type is ‘-as [Int]’, the benefit is that it reduce values with lots of decimal places to integers.


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

 


See more Windows PowerShell flow control examples

PowerShell Switch Statement  • PowerShell Real-life Techniques  • Free Permissions Analyzer

Differences between For, ForEach and ForEach-Object  • PowerShell Loops  • PowerShell Home

Conditional Operators   • Do While Loop  • PowerShell If Statement  • PowerShell Brackets

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.