Use PowerShell to Find PowerShell Type Accelerators

Use PowerShell to Find PowerShell Type Accelerators

  • Comments 9
  • Likes

Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to find built-in type accelerators.

Microsoft Scripting Guy, Ed Wilson, is here. Hmm…today is a lousy Monday after a big holiday weekend in Charlotte, North Carolina in the United States. I am still somewhat in holiday mode, which means I am up early, sipping a cup of English Breakfast tea with a bit of lemon grass and a cinnamon stick. I am checking the email sent to scripter@microsoft.com. Actually, the Scripting Wife just came in and reminded me that today is not a holiday for me, so the question becomes what am I going to do differently with that knowledge? Really nothing. It is hard to tell the difference between a holiday and a normal day when one is addicted to Windows PowerShell. I have a blog post that I have been wanting to write. Therefore, here goes…

Understanding PowerShell type accelerators

Whenever I show someone about using a type accelerator in Windows PowerShell, they get all excited, and then they immediately ask, “How can I find out about all of the available type accelerators?” I can point them to my MS Press Windows PowerShell 2.0 Best Practices book because it contains a list of the type accelerators, but that is perhaps overkill. There is also a list on the Windows PowerShell Team blog, but that was written a long time ago.

So what are type accelerators? Well, they are aliases for .NET Framework classes, and therefore, they make those classes easier to use inside Windows PowerShell. For example, one type accelerator is for the System.DateTime .NET Framework class, which is shown here:

[datetime]

To get the current date and time, use the Now static property. This is shown here:

PS C:\> [datetime]::now

Sunday, July 7, 2013 11:51:43 AM

Many of the type accelerators are fairly simple, and they do not provide that much of a shortcut. For example, the type accelerator for System.Boolean is [bool]. It allows me to cast a number to a Boolean value. Here is an example of using this accelerator:

PS C:\> [bool]2

True

PS C:\> [bool]1

True

PS C:\> [bool]0

False

PS C:\> [bool]-1

True

Although this is cool, it is not something I use every day. On the other hand, some sophisticated type accelerators make working with things like WMI much more efficient, and greatly reduce the amount of typing. Here is a listing of the WMI type accelerators.

wmi                    System.Management.ManagementObject

wmiclass               System.Management.ManagementClass

wmidatetime            Pscx.TypeAccelerators.WmiDateTime

wmisearcher            System.Management.ManagementObjectSearcher

Finding type accelerators by using PowerShell

In Windows PowerShell 2.0, it was possible to use the System.Management.Automation.TypeAccelerators class, and find the enumeration values.

Tip: For more information about enumerations, see this collection of Hey, Scripting Guy! Blogs.

That .NET Framework class is private, and therefore in Windows PowerShell 3.0, it is more difficult to gain access to the values. Joel Bennett posted a suggestion for a bug on Microsoft Connect, where he showed how to use the [psobject] type accelerator to gain access to the class:

[psobject].Assembly.GetType("System.Management.Automation.TypeAccelerators")::get

This is pretty cool. But Windows PowerShell 3.0 also introduces a type accelerator for type accelerators. Yep, and it is very easy to use. Here is the command:

[accelerators]::get

Now, whenever I get ready to use a type accelerator, the first thing I do is put it in square brackets, and press ENTER. If it resolves, the accelerator will work. Here is an example:

PS C:\> [accelerators]

 

IsPublic IsSerial Name                                     BaseType

-------- -------- ----                                     --------

False    False    TypeAccelerators                         System.Object

Here is a screenshot of using the [accelerators] type accelerator to get a list of the type accelerators on my system.

Image of command output

Join me tomorrow when I will talk about more cool Windows PowerShell type accelerators.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • I have Windows 8 and 3.0 according to $host but the [accelerators]::get thing doesn't work.  "Unable to find type [accelerators]: make sure that the assembly containing this type is loaded."  How come?

  • I'm with Casey on this one...  Windows 8 Enterprise

  • Ed,

    Do you have the PowerShell Community Extensions installed? That's where [Accelerators] comes from on my machine...

  • It comes from here:

    $xlr=[psobject].Assembly.GetType('System.Management.Automation.TypeAccelerators')

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

    [accelerators]::get

    Always available in Windows 7 and later.

  • The following works in all versions of PowerShell V2:

    $xlr=[psobject].Assembly.GetType('System.Management.Automation.TypeAccelerators')

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

    [accelerators]::get

  • I can run:

    [psobject].Assembly.GetType("System.Management.Automation.TypeAccelerators")::get

    Without any issue.  Reading above in, I should be able to simply time in [accelerators] without adding any additional modules.  At least that is how I read it.

    [PS] C:\Users\my_id> [accelerators]

    Unable to find type [accelerators]: make sure that the assembly containing this type is loaded.

    At line:1 char:1

    + [accelerators]

    + ~~~~~~~~~~~~~~

       + CategoryInfo          : InvalidOperation: (accelerators:TypeName) [], RuntimeException

       + FullyQualifiedErrorId : TypeNotFound

  • @all yes I have the PowerShell Community Extension Project installed on my Windows 8 laptop, and it seems this is where the [accelerators] type accelerator comes from. Sorry for the confusion.

  • Thanks for the clarification! I thought I was losing my mind.