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.
Avatar

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.

Integer type accelerator

Integer type accelerator

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)
PSCredential type accelerator

PSCredential type accelerator

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
List all type accelerators

List all type accelerators

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}
PSObject type accelerator

PSObject type accelerator

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]
IP address type accelerator

IP address type accelerator

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/"
URI type accelerator

URI type accelerator

Another example is the [datetime] type accelerator you can use to convert strings to DateTime objects.

[datetime] '05-01-1991'
[datetime].FullName
The datetime type accelerator

The datetime type accelerator

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)
TimeSpan type accelerator

TimeSpan type accelerator

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)
Void type accelerator

Void type accelerator

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*'})
Creating a new type accelerator

Creating a new type accelerator

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*'})
Remove a type accelerator

Remove a type accelerator

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.

avataravatar
2 Comments
  1. Avatar
    Durgesh 5 years ago

    Simply great. Easy to understand.

  2. Avatar
    Luke Avedon 4 years ago

    This is great.  Thank you so much!

Leave a reply

Your email address will not be published. Required fields are marked *

*

© 4sysops 2006 - 2023

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account