Adding New Type Accelerators

Share |

Join PowerShell.com!
Subscribe to Feed

PowerShell eBook
Sign up for
Your PowerTip of the Day:

Award-winning PowerShellPlus


Admin Guide to PowerShell Remoting


Mastering PowerShell eBook

To access popular .NET types faster, PowerShell maintains a list of shortcuts called "type accelerators". That's why you can use the term "XML" for XML data instead of having to always write "System.Xml.XmlDocument":

PS> [xml].FullName
System.Xml.XmlDocument

The list of type accelerators can be extended, so if you have additional .NET types that you use often, you can add them to that list.

This line adds a new type accelerator called "Path" that refers to the type "System.IO.Path":

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

You can now access all the highly useful static methods in that type very easily:

PS> [Path]::GetFileNameWithoutExtension('test.txt')
test

Note that type accelerators are just for your convenience. You can always use the full type name instead, too:

PS> [System.IO.Path]::GetFileNameWithoutExtension('test.txt')
test

Twitter This Tip! ReTweet this Tip!


Posted Feb 12 2013, 06:00 AM by ps1
Copyright 2012 PowerShell.com. All rights reserved.