The PowerShell Command Prompt
Windows PowerShell prompts for commands just as the old Command Prompt environment does. It prompts, you type a command, you press Enter, it displays some sort of results, and you repeat the process over and over.
One thing that’s really improved with PowerShell, though, is that the command language is really a full-fledged programming language and creating, working with, and displaying variables is easy. For example, you might recall that you could use the set name=xxx
command in batch files to store data in named variables and use the %name%
syntax to retrieve the data.
In Windows PowerShell, you define variables by putting a dollar sign ($) in front of the variable’s name to both define and retrieve its value. For example, you can type the commands
$a = 40 + 7 $a
and Windows PowerShell obediently displays the computed value of a, which is 47.
Every PowerShell command is just an expression with a value, so it’s perfectly legal to type 40+7
as a command line. PowerShell prints the result. These are trivial examples, but it can be quite powerful when you’re working with objects—just type the name of an object and its property to display the value of the property, for example:
$f.path
to print the Path property of an object you’ve put into variable $f.
Remember that whatever you can put into a script, you can also type directly at the PowerShell command prompt.
Command-Line Editing
PowerShell lets you use the backspace, arrow, and other editing keys on your keyboard to modify command lines as you’re typing them and to recall, modify, and reuse previously typed commands. This will be very familiar if you’ve worked with the Command Prompt because the editing keys are identical. (There’s a reason for this: PowerShell is a standard console application just like cmd.exe.)
The arrow key functions are the most important to remember:
- The left and right arrow keys (← and →) let you move within a command line that you’re typing, so you can go back and change a mistake at the beginning of the line without erasing and retyping the whole thing.
- Ctrl+← and Ctrl+→ move back and forth a whole word at a time. This can save a lot of keystrokes.
- The Home and End keys move instantly to the beginning or end of the line.
- Normally, if you move backward in the edited line and start typing, what you type is inserted ahead of what’s already there. This is usually very handy. If you need to replace a lot of text, though, instead of erasing what’s there and then typing in the new, press the Ins key once and your typing replaces what’s already there—just type over it. The cursor changes to a solid block to remind you that this is what will happen when you type. You can press Ins again to return to Insert mode. PowerShell automatically switches back to Insert mode when you press Enter, so you can edit the next command normally.
- The up and down arrow keys (↑ and ↓) let you step back to commands you typed previously. You can use ↑ to locate a previous command and press Enter to reissue it, or you can locate it and then edit it before pressing Enter.