Wednesday, May 14, 2008

PowerShell Type Shortcuts

One of the coolest and most useful tools in Powershell is the "Type Accelerators" or "Type Shortcuts" as some might call them. What "Type Accelerators" do is reduce the amount of typing you need to do in order to create an object of that particular .Net Type. It was mostly created for the Shell environment as most admins want to limit their keystrokes while typing in the shell. Although typing the accelerator in the shell or in a script produces the same result.

Now I know that this is an old subject for most experienced Powershell users but it is also greatly taken for granted. The powershell team has really made creating objects of mostly used .Net types very easy. And if you wish you can create your own type accelerators. The list is quite long, but here is a list of the types I use most often.

[int]
[regex]
[string]
[xml]
[bool]
[char]
[array]
[wmi]
[wmiclass]
[wmisearcher]
[switch]
[void]

Keep in mind that the type accelerators for an array of most of these types can be represented by putting an opening and closing square brackets right after the type. For example.

[int[]]
[string[]]

So for a Boolean type accelerator or type shortcut array you would type:

[bool[]]

Pretty easy right?

Ok, but what if you wanted to create your "Own" type accelerator for a type that you use a lot? For example what if you use the "io.File" type quite often? No problem.

$ft = [System.IO.File]
$ft::ReadAllText("c:\sample.txt")

Like Borat would say, "Very Nice!!"

No comments: