Quick Hits: Finding, Creating and Removing PowerShell Type Accelerators

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

image

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

image

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.

image

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.

image

About what I expected. Now I will create a new type accelerator and try this again.

$accelerators::Add('tcpclient','net.sockets.tcpclient')

image

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.

$accelerators::Get['tcpclient']

image

Removing a type accelerator is just as easy using the Remove() method. Just supply the name of the type accelerator.

image

Let’s remove the [tcpclient] accelerator that I just created.

$accelerators::Remove('tcpclient')

image

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!

image

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:

$accelerators::Add('accelerator',$accelerators)

image

That is all to get you started on exploring PowerShell type accelerators.

This entry was posted in powershell, Tips and tagged , , , , . Bookmark the permalink.

3 Responses to Quick Hits: Finding, Creating and Removing PowerShell Type Accelerators

  1. Rebecca says:

    Interesting article. We’ve written a new blog on Powershell Type accelerators – shortcuts to .Net classes that might be of interest: http://www.ravn.co.uk/powershell-type-accelerators-shortcuts-dotnet-classes/

  2. Pingback: Quick Post – Creating SMO Type Accelerators | $hell Your Experience !!!

  3. Ryan Dennis says:

    Reblogged this on SharePointRyan.com and commented:
    Great post by Boe Prox on PowerShell Type Accelerators!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s