- Create a custom role with Azure role-based access control (Azure RBAC) using PowerShell - Wed, Jan 20 2021
- Step by step Deploying Docker Container to Azure using Azure CLI - Wed, Sep 2 2020
- Install Docker offline on Windows Server 2016 - Thu, Dec 6 2018
PowerShell type accelerators are aliases for .NET classes or types, which makes using classes in PowerShell scripts much easier. The intention behind type accelerators is to use shorter names for .NET classes and types and save some unnecessary typing. For example, when you use the type accelerator [int] to define an integer value, there's actually no data type called int. Instead, it's just an alias for the [System.Int32] class.
Similarly, you can also create PowerShell credentials using the type accelerator [PSCredential]. This is much shorter to type and an alias of the System.Management.Automation.PSCredential class, like in the following code example:
$password = ConvertTo-SecureString 'Password@123' -AsPlainText -Force New-Object System.Management.Automation.PSCredential ('Administrator', $password) # Alternatively create credentials using the type accelerator [PSCredential]::new('Adminstrator', $password)
Listing all type accelerators
PowerShell has type accelerators built in. You can even query them using the following command, which will return all type accelerators in your PowerShell console.
[PSObject].Assembly.GetType('System.Management.Automation.TypeAccelerators')::Get
Useful type accelerators
PowerShell type accelerators can be very handy in some scenarios to save a lot of typing. The following are some common use cases.
For instance, a simpler way to create a PowerShell object using the [PSCustomObject] type accelerator instead of the System.Management.Automation.PSObject is by passing a hash table to typecast it to a PowerShell object.
[System.Management.Automation.PSObject]@{Name='Prateek';Age=28} # using type accelerators [PSCustomObject]@{Name='Prateek';Age=28} [PSObject]@{Name='Prateek';Age=28}
You can use the type accelerator [IPAddress] to validate IP addresses. This is also an alias of the System.Net.IPAddress class. It will throw an error when the IP is out of range or in an incorrect format.
[IPAddress] '127.0.0.1' [IPAddress] '127.0.0.300' [IPAddress] '127.0.0.abcd' '127.0.0.1' -as [IPAddress] '127.0.0.abcd' -as [IPAddress]
Similarly, you can also use the [URI] type accelerator to deconstruct URLs to extract the host, local path, or the port number.
[uri] "https://4sysops.com/archives/managing-disks-with-powershell/"
Another example is the [datetime] type accelerator you can use to convert strings to DateTime objects.
[datetime] '05-01-1991' [datetime].FullName
You can even create time periods with the [TimeSpan] type accelerator and perform operations on them. In the following example, I'm adding a time span to the current date/time to create a future date.
# [TimeSpan]::New(days, hours, minutes, seconds) $ts = [TimeSpan]::New(1, 7, 5, 59) (Get-Date).AddHours($ts.TotalHours)
The type accelerator [void] can suppress all of your console outputs:
$array = New-Object System.Collections.ArrayList $array.Add(1) $array.Add(2) [void] $array.Add(3)
Creating your own type accelerators
PowerShell type accelerators are not only limited to in-built accelerators. The list is extensible, which means you can create your own custom type accelerators. The following code sample demonstrates how to create an alias for the System.Net.Sockets.TCPClient class and name it [TCP] using the Add() method. Now you can create a new instance of this class by just using the type accelerator like [TCP]::New() and passing the arguments.
$Accelerators = [PowerShell].Assembly.GetType("System.Management.Automation.TypeAccelerators") # add a new type accelerator $Accelerators::Add("TCP","System.Net.Sockets.TCPClient") # use the type accelerator [tcp]::New('localhost',135) # add it to PowerShell type accelerators $Accelerators::get.GetEnumerator().Where({$_.Key -like 'T*'})
PowerShell also lets us remove type accelerators once they are no longer required.
Subscribe to 4sysops newsletter!
# Removing a type accelerator $Accelerators::Remove('TCP') # Type accelerator no longer exists $Accelerators::get.GetEnumerator().Where({$_.Key -like 'T*'})
Wrap-up
PowerShell type accelerators are just aliases for .NET classes. They are shorter to type and easy to comprehend in PowerShell scripts. You can use them to perform powerful operations such as data typecasting, validating, or creating new objects of the class. PowerShell type accelerators are extensible, which means you can write your own class aliases and remove them once they are not required.
Simply great. Easy to understand.
This is great. Thank you so much!