PowerShell Command Shell Tips and Tricks

Learn these 10 indispensable techniques for command-shell mastery

Michael Otey

April 26, 2010

3 Min Read
ITPro Today logo in a gray background | ITPro Today

Windows PowerShell is Microsoft's preferred scripting language, and most of Microsoft's server products now have PowerShell cmdlets so that you can manage them with PowerShell scripts. However, PowerShell is more than a scripting language. PowerShell also has its own command shell. Learning the shell's nuances is essential for productive PowerShell work. In this column, you'll learn 10 indispensable techniques for working in the PowerShell command shell.

10. Redisplay the last command—To redisplay the last command you entered in the PowerShell command shell, press the up arrow key. Continuing to press this key scrolls through the history of the commands entered. Use the down arrow key to scroll back through the list of commands.

9. Replay previous input—While the up and down arrow keys let you scroll through the entire previous command line, the right arrow key allows you to quickly enter the characters from the previous command. Pressing the right arrow displays the characters from the previous command one character at a time.

8. Use QuickEdit to copy text—Although it's not obvious, the PowerShell command shell lets you select and quickly copy any text displayed in the command shell. Use the mouse to select the text to be copied, then press Enter or right-click on the selected text to copy it to the clipboard. You need to enable QuickEdit Mode on the Options tab of the PowerShell Properties dialog box to take advantage of this feature.

7. Right-click the mouse to paste into the command shell—The standard Ctrl+v paste command doesn't work inside the PowerShell command window. Instead, after you've copied text to the clipboard, you position the mouse at the command shell command prompt and just right-click to paste the contents of the clipboard to the input line.

6. Use tab for auto completion—Using the tab key as you type commands causes the PowerShell command window to attempt to complete the commands you're typing. For instance, entering

Get \[tab\]

displays the first PowerShell Get- cmdlet alphabetically, which is Get-Acl. Continuing to press tab cycles through all the available Get- cmdlets.

5. Recognize and escape from incomplete input—If you enter a PowerShell command but the statement isn't complete, the command shell displays its incomplete input prompt, >>, which Figure 1 shows. You can then complete the command, or you can cancel the current command or input request by pressing Ctrl+c. 


Figure 1: Code showing PowerShell's incomplete input prompt

 

4. Create variables without a script—You might think that variables can be created only inside scripts, but that's not the case. You create a variable in the command shell simply by prefixing the $ symbol to a name:

$server = "MyServer"

3. Use piping to chain commands together—Piping uses the pipe separator symbol (|) to send the output of a command to the input of another command. Piping works with all PowerShell commands. The following example shows how you can pipe the output of the dir command to Sort-Object in order to sort the output according to file size:

dir | sort-object -property length, name

2. Use redirection to send a command's output to a file—Redirection lets you direct the results of a command to a file. You use redirection by adding > to the end of a command, followed by the path for where you want the output to go. This example shows how you can redirect the output of the dir command to a file called mydir.txt:

dir > c:tempmydir.txt

1. Use Properties to customize the command shell—To change the PowerShell command shell properties, click the PowerShell icon displayed in the upper left corner of the title bar of the command shell window and select Properties to open the Properties dialog box. The Layout tab, which Figure 2 shows, lets you change the screen size, the Options tab lets you change the command buffer size, and the Colors tab lets you change the command shell's font and background colors.


Figure 2: The Layout tab of the PowerShell Properties dialog box

 

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like