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.

Prateek Singh

Prateek Singh is an infrastructure developer, an avid PowerShell blogger, and a community contributor. In 2017, FeedSpot recognized his blog RidiCurious.com as among the "Top 50 PowerShell blogs in the world." All of his PowerShell projects and modules are open-sourced at GitHub, and you can reach out to him on Twitter at @SinghPrateik.

Latest posts by Prateek Singh (see all)

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:

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.

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.

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.

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

URI type accelerator

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

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

TimeSpan type accelerator

The type accelerator [void] can suppress all of your console outputs:

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.

Creating a new type accelerator

Creating a new type accelerator

PowerShell also lets us remove type accelerators once they are no longer required.

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.

Join the 4sysops PowerShell group!

Your question was not answered? Ask in the forum!

5+

Users who have LIKED this post:

  • avatar
  • avatar
Share
2 Comments
  1. Durgesh 8 months ago

    Simply great. Easy to understand.

    0

  2. Luke Avedon 3 months ago

    This is great.  Thank you so much!

    0

Leave a reply

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

*

© 4sysops 2006 - 2019

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