Adding New Type Accelerators in Powershell

by Sep 11, 2013

If you find yourself using certain .NET types frequently, you may want to make your life easier and implement shortcuts.

For example, there is a .NET type called "System.IO.Path" with a lot of useful path functionalities:

[System.IO.Path]::GetExtension('c:\test.txt')
[System.IO.Path]::ChangeExtension('c:\test.txt', 'bak')

If you feel tired typing the long .NET type name all the time, simply add a shortcut like so:

[PSObject].Assembly.GetType("System.Management.Automation.TypeAccelerators")::Add('Path', [System.IO.Path])

Now, you can access the very same functionality via your new shortcut "Path":

[Path]::GetExtension('c:\test.txt')
[Path]::ChangeExtension('c:\test.txt', 'bak')

To get a list of all methods and properties supported by a type, try this:

[Path] | Get-Member -Static

Twitter This Tip! ReTweet this Tip!