Use PowerShell to Find PowerShell Type Accelerators

Doctor Scripto

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 

0 comments

Discussion is closed.

Feedback usabilla icon