Learn about Windows PowerShell
Summary: Learn how to use Tab Expansion in the Windows PowerShell ISE to avoid using cmdlet aliases and to add complete parameter names in commands.
Hey, Scripting Guy! I spend a decent amount of time working with scripts, and I would like to learn how to use the Windows PowerShell ISE more effectively. I have heard that I should not use aliases in scripts, but it just takes too long to type out full cmdlet names—after all, some of the cmdlet names are ridiculously long and hard to type. I notice that you seem to use complete cmdlet names, do you have a secret, or are you just a really good typist?
Microsoft Scripting Guy, Ed Wilson, is here. Actually, DJ, I am a fairly good typist, I made an “A” in typing class when I was in school (a little slow with numbers, but other than that, I’m pretty good). I also wrote a really cool Windows PowerShell function to Modify the PowerShell ISE to Remove Aliases from Scripts. I load this function with my Windows PowerShell ISE Profile that replaces Windows PowerShell aliases with full cmdlet names. By using this function, I am able to write my code using aliases. I then call the function, and it cleans up my code and copies the modified code to a new Windows PowerShell ISE script pane. It does not modify the original code, so there is no worry if it were to make a mistake.
Note This is the fourth blog in a series about using the Windows PowerShell ISE.
For example, the following code works just fine; but it contains a number of aliases, and it is not a good practice to have these in a script.
select name, id -First 3 |
ft name, id –AutoSize
I run this code to ensure that it works. It does, and both the code and the output will appear in the output pane (because I have not saved this script yet). I then use the Remove-AliasFromScript function (loaded with my Windows PowerShell ISE profile) to remove all the aliases from the script.
The newly created script (without any aliases) appears in a new Windows PowerShell ISE script pane as untitled5.ps1. When I test it and decide I like the new script, I can save it with the name of my choosing. In the image that follows, you can see the original code (in the output pane), the output associated with the script, the Remove-AliasFromScript command, and the revised script in the new script pane.
Well, suppose that you do not want to use my Remove-AliasFromScript function. How do you avoid typing lots of long cmdlet names? The secret is to use the Windows PowerShell ISE Tab Expansion feature. Tab Expansion does not expand aliases, so if you type gps an alias for the Get-Process cmdlet, it will not resolve gps to Get-Process (that is what my Remove-AliasFromScript function does).
What Tab Expansion does is complete the cmdlet name when you type a portion of the name. One thing to keep in mind is that Tab Expansion is not a mind reader. So, if I type Get- and then I press the Tab key <tab>, Get-ACL appears in the ISE on my computer. If I press the Tab key again, Get-Alias appears. Each time I press the Tab key, the Windows PowerShell ISE cycles through the next cmdlet in order. So, if I wanted to use Get-Process that would be a lot of pressing of the Tab key.
The secret is to type just a little bit more of the cmdlet name prior to pressing the Tab key. On my computer, if I type Get-p and then I hit the Tab key, the first cmdlet that appears is Get-PfxCertificate. When I press the Tab key again, Get-Process appears, and I can now use the command. If I want to ensure that I go directly to the Get-Process cmdlet, I type Get-pr and then press the Tab key.
Note Keep in mind that these examples for using Tab Expansion all depend on what modules, cmdlets, version of Windows, and roles are installed. When you have lots of cmdlets available, the usefulness of Tab Expansion begins to bog down.
Tab Expansion also works for parameters. One thing that my Remove-AliasFromScript function does not do is add in missing parameters to commands. The use of parameters is essential to learning how Windows PowerShell actually works. When I was first learning Windows PowerShell, there were many times when I thought that my pipelined input was supplied to a particular parameter, only to learn later that the pipelined input was going somewhere else. At other times, I would see a command fail, such as the one that is shown here, and I wondered why it did not work.
Later, I would discover a command that works, such as the one shown here.
Get-Process notepad | Stop-Process
And although the command works, it does nothing to help me learn why the one command works, and the other one does not work. Later, I figured out that the default parameters of Get-Process and Stop-Process are reversed. But if I use the parameter names, it does not matter. This is shown here.
Get-Process -Name notepad
Stop-Process -Id 3576
The command to start Notepad, get all instances of Notepad, and then stop a specific instance of Notepad is shown in the image that follows.
When you use the Windows PowerShell ISE, it is easy to add parameters to a command. After completing the cmdlet name (by using the Tab key or typing the cmdlet name), type a dash and press the Tab key. It will then populate the first parameter name. If this is the parameter you want, you can use it. However, if you do not want that parameter, press the Tab key again and again until you find the parameter you want.
If you find a parameter you want, but you accidently go past it, you do not have to continue to cycle through all of the available parameters. You can hold down the Shift key <shift> and press the Tab key, and it will cycle backwards. This allows you to come back to a parameter you may have missed in the cycle. This Shift + Tab technique also works when using Tab Expansion for cmdlet names.
DJ, that is all there is to using Tab Expansion in the Windows PowerShell ISE to assist in typing in complete cmdlet names and parameter names. This also concludes Windows PowerShell ISE Week. Join me tomorrow as I reveal the 2012 Scripting Games Frequently Asked Questions (FAQ).
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
TAB Expansion is quite helpful and I make often use of it!
One thing I'm missing is that it doesn't work if I split the commandline on different physical lines like
This seems to stop it from working, so I usually have to build a long line and split it later on :-(
@K_Schulte you are right, it does not seem to work when the command is split. It also seems to break down when I have grouped expressions that when taken together form the value for an argument. But it is very useful, and I wish that Microsoft Word had Tab Expansion as well :-)