Prof. Powershell
PowerShell Tip: Utilizing the –As Operator
Here's how to get PowerShell to treat an object or command as a different object or command.
- By Jeffery Hicks
- 08/13/2013
In the previous article we looked at some shortcuts for defining objects of a specific type. Those techniques work just fine at the time you are defining or creating the object. But another way to work with object types is to instruct PowerShell to treat something as something else. In these situations we can use the –As operator.
For example, you might have a string:
PS C:\> $s = "7/4/1976"
To treat this as a datetime object is as simple as, well, using –AS:
PS C:\> $s -as [datetime]
Sunday, July 4, 1976 12:00:00 AM
You might use something like this with Read-Host.
PS C:\> $c = Read-host "Enter the name of a wmiclass"
Enter the name of a wmiclass: win32_service
PS C:\> $c -as [wmiclass]
NameSpace: ROOT\cimv2
Name Methods Properties
---- ------- ----------
Win32_Service {StartService, St... {AcceptPause, AcceptStop, Captio...
You can use –AS with type accelerators or just about any class name.
PS C:\> $svc = "bits" PS C:\> $svc -as [serviceprocess.servicecontroller]
Status Name DisplayName
------ ---- -----------
Running bits Background Intelligent Transfer Ser...
PS C:\> 1 -as [bool]
True
PS C:\> 0 -as [bool]
False
PS C:\> "172.16.45.123" -as [ipaddress]
Address : 2066550956
AddressFamily : InterNetwork
ScopeId :
IsIPv6Multicast : False
IsIPv6LinkLocal : False
IsIPv6SiteLocal : False
IPAddressToString : 172.16.45.123
There's no guarantee this will always work, or that you will get the results you expect, so the best thing to do is just try it.
In addition to treating something as something else, you can also ask PowerShell to test if something is of a specific type. Can you guess the operator?
PS C:\> $t -is [int]
False
PS C:\> $t -is [string]
True
Use this operator in your scripts to help validate input and data to make sure it is of the expected type. The –IS operator will return True or False which makes it easy to use in an IF statement.
PS C:\> $a = get-process powershell
PS C:\> if ($a -is [array]) {"found multiple powershell processes"}
found multiple powershell processes
PS C:\> $a
Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessNam
------- ------ ----- ----- ----- ------ -- ----------
472 30 123080 95504 638 2.01 1240 powershell
522 35 173292 28152 822 359.16 4512 powershell
If you want to test the opposite there is a –IsNot operator.
PS C:\> 4 -is [int]
True
PS C:\> 4 -isnot [int]
False
Although I would image the use case for this might be limited. Most of the time I'm more interested to know if something is a certain type.
Finally, let's wrap up this lesson with some PowerShell commands that will show you all of the available type accelerators on your computer. In PowerShell 2.0 you can run these commands:
$acceleratorsType = [type]::gettype("System.Management.Automation.TypeAccelerators")
$acceleratorsType::Add("accelerators", $acceleratorsType)
[accelerators]::Get
You will get a hashtable of accelerators and their values.
Key Value
--- -----
Alias System.Management.Automation.AliasAttribute
AllowEmptyCollection System.Management.Automation.AllowEmptyCollect...
AllowEmptyString System.Management.Automation.AllowEmptyStringA...
AllowNull System.Management.Automation.AllowNullAttribute
array System.Array
bool System.Boolean
…
In PowerShell 3.0 you can do this in a single command.
[psobject].assembly.gettype("System.Management.Automation.TypeAccelerators")::Get
Once you know the accelerator you can use it to quickly define a new object, test if an object is that type or cast an object to that type. Just remember that these are accelerators or shortcuts and not an exhaustive list of all possible object types.
About the Author
Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.