I saw a question recently that somebody asked on how to locate and create custom type accelerators without the need of the PowerShell Community Extensions (PSCX) and its type accelerator, [accelerator]. I quickly answered his question, but decided that this would make a decent article not only for everyone, but also for my own reference.
The quick synopsis of a type accelerator is that it is basically a shorthand type for a full .Net type such as System.Text.RegularExpressions.Regex in which the type accelerator is [regex].
As you can see, both accomplish the same thing, just [regex] is a lot shorter to work with. So how can we see all of available type accelerators using PowerShell? Just run the following command to see them:
$accelerators = [PSObject].Assembly.GetType('System.Management.Automation.TypeAccelerators') $accelerators::Get
I created a TypeAccelerator type and then use the Get property to show both the type accelerator as well as the full type name that it references.
Now the next question is, “How can I create my own type accelerator?”. Simple, you can use the existing TypeAccelerators object created and make use of the Add() method.
All I need to do is provide the type accelerator name along with the type that I want to shorten. For instance, I want to shorten net.sockets.tcpclient down to [tcpclient]. Initially, if I try to run it, I see this great message.
About what I expected. Now I will create a new type accelerator and try this again.
Look at that! Works like a champ. Now I have a new type accelerator that I can use instead of typing out the full name. And I will also see this type accelerator in the list as well.
Removing a type accelerator is just as easy using the Remove() method. Just supply the name of the type accelerator.
Let’s remove the [tcpclient] accelerator that I just created.
I’ll receive a boolean value of True telling me that this was removed successfully and when I try to locate the [tcpclient] type accelerator, it is no longer available. Can I remove a system generated type accelerator such as [xml]? Sure!
Would I want to? Not really because you may need it at some point during your session. Of course you can either re-create it or just start a new PowerShell session to get it back. Also, to create your own [accelerator] that was mentioned at the beginning of this article, just do this:
That is all to get you started on exploring PowerShell type accelerators.