Learn about Windows PowerShell
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 firstname.lastname@example.org. 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…
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:
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
PS C:\> [bool]1
PS C:\> [bool]0
PS C:\> [bool]-1
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.
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:
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:
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.
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 email@example.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy
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
Do you have the PowerShell Community Extensions installed? That's where [Accelerators] comes from on my machine...
It comes from here:
Always available in Windows 7 and later.
The following works in all versions of PowerShell V2:
I can run:
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
+ 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.