PowerShell for the People: Making the shell work for you

021913_2047_WordTest1.png Once you have some basic PowerShell experience I think you will begin looking for all sorts of ways to use PowerShell. Although one of the biggest obstacles for many IT Pros is the thought of having to type everything. Certainly, PowerShell has a number of features to mitigate this, often misperceived, burden such as tab completion and aliases. It is the latter that I want to discuss today.

An alias is simply an short command name alternative. Instead of typing Get-Process you can type ps. Even I can’t screw that up. Using aliases at the command prompt is a great way to speed up your work and cut down on typos. While I wouldn’t type this in a script:

At a prompt, it is quick and I get the desired results. However, you can’t create an alias for a command and its parameters. For example, I love the -First and -Last parameters with Select-Object. But I can’t create an alias equivalent to Select-Object -last 10. What you can do however, is create your own wrapper around a command and put an alias to that. Here is a function I wrote for Select-Object -Last X.

You’ll notice that the last part of my code snippet is defining an alias called Last that references this function. But now I have something quick and easy to use at a PowerShell prompt.

You’ll have to figure out the best way to wrap the cmdlet. In this case, I am taking each piped in object and adding it to an array, only keeping the specified number of items. As each item is added, the other items are moved “up” in the array.

Be aware that there may be a trade-off between convenience and performance. This command using my alias and custom function:

took 237ms. Whereas the Select-Object approach:

Only took 49ms. But I bet you might be willing to accept that tradeoff to save some typing. Of course, if there is a Last command there should be a First command.

Performance-wise this is easier because all I have to do is count piped in objects and bail out once I reach the limit.

In this example I’m not only taking advantage of aliases, but also positional parameters and only having to type enough the of the parameter name so PowerShell knows what I want.

So there are ways to make PowerShell more keyboard friendly, although it might take a little work on your part. Next time we’ll look at another alternative.

Get PowerShell Parameter Aliases

magnifying-glass During a recent PowerShell training class we naturally covered aliases. An alias is simply an alternate name, often something that is shorter to type, or maybe even more meaningful. There are aliases for commands, properties and parameters. Discovering aliases for commands is pretty easy with Get-Alias. Property aliases are discoverable using Get-Member. But, discovering parameter aliases is a bit more difficult. The information is there, but doesn’t surface very well. It would be terrific if help showed parameter aliases but it rarely does. So here are some ways you might find parameter aliases.

One way is to use Get-Help.

But you need to use Get-Help. If you use the Help function it won’t work. In the command I filtered out parameters that didn’t have aliases. You can also pipe Get-Command to Get-Help.

But for some reason, this doesn’t always work. There are aliases for Get-Service, but these same commands fail to show it.

I know there are aliases because Get-Command shows me, although it takes a little work to extract this information.

Plus I can verify at the prompt:

Since it appears I can always get the information from Get-Command, I wrote a function called Get-ParameterAlias.

The function can take a command name or you can pipe something from Get-Command.

Because parameter information from Get-Command includes common parameters such as -ErrorAction, I’ve skipped those by default, unless you use the -IncludeCommon parameter.

Now it is easy to discover parameter aliases for say a module.


Knowing parameter aliases can make you more efficient in the console. But remember, when committing PowerShell to a script use the full parameter name as some of these aliases can be a bit cryptic.

As always, I hope you’ll let me know what you think.

Convert Aliases with the Tokenizer

Last week I posted a function you can use in the Windows PowerShell ISE to convert aliases to command definitions. My script relied on regular expressions to seek out and replace aliases. A number of people asked me why I didn’t use the PowerShell tokenizer. My answer was that because I’m not a developer and don’t think that way. But after working a bit with it I can see the value so I have another function you can use in the ISE to convert aliases to commands. Continue reading

PowerShell ISE Convert All Aliases

Yesterday I posted an article on how to convert a selected word to an alias or cmdlet. While I think there is still some value in this piecemeal approach. sometimes you want to make wholesale changes, such as when troubleshooting a script that someone else wrote that is full of cryptic aliases. I have a function you can integrate into the ISE that will convert all aliases in a block of selected text and convert them to their full cmdlet name equivalents. Continue reading

PowerShell ISE Alias to Command

Earlier this week I posted a function that you could incorporate into the PowerShell ISE to convert selected text to upper or lower case. I was challenged to take this a step further and come up with a way to convert aliases to commands. Which is exactly what I did. Continue reading