Skip navigation
Using PowerShell Interactively

Using PowerShell Interactively

While PowerShell is Powerful a scripting language, it’s also an interactive command shell that you can use instead of Cmd.exe.

Last time, I talked about the interpretations of special characters in Cmd.exe and PowerShell. It’s important to understand that PowerShell has a number of significant differences between the two programs, but it’s also important to understand the “shell” part of PowerShell. While PowerShell is Powerful a scripting language, it’s also an interactive command shell that you can use instead of Cmd.exe.

Getting Started

Windows provides two PowerShell interfaces: The standard console mode version (powershell.exe), and the PowerShell Integrated Scripting Environment (ISE). I’m only going to discuss only the console version of PowerShell, because it is the most similar to Cmd.exe. You can start the console version of PowerShell by choosing the Windows PowerShell program shortcut (rather than the Windows PowerShell ISE shortcut).

When you start PowerShell, it displays a command-line interface that is superficially similar to Cmd.exe, except that the prompt line starts with PS to remind you that you’re using PowerShell. Just like Cmd.exe, PowerShell is waiting for you to enter a command:

PS C:\Users\kendyer>

PowerShell comes preconfigured with a number of compatibility aliases that are designed to ease your transition from other command-line shells, such as Cmd.exe. (An alias allows you to define an alternate name for a PowerShell cmdlet, function, or other command.) For example, In Cmd.exe, we use the Cd (or Chdir) command to change directories. The PowerShell cmdlet is Set-Location, but Cd is an an alias for Set-Location. I have listed the most common Cmd.exe command aliases in Table 1.

PowerShell Alias

PowerShell Command

Cd or Chdir

Set-Location

Cls

Clear-Host*

Copy

Copy-Item

Del or Erase

Remove-Item

Dir

Get-ChildItem

Echo

Write-Output

Md

mkdir*

Move

Move-Item

Pushd and Popd

Push-Location and Pop-Location

Rd or Rmdir

Remove-Item

Ren

Rename-Item

Sort

Sort-Object

Start

Start-Process

Type

Get-Content

* These are predefined functions rather than cmdlets

Table 1: Most common PowerShell Cmd.exe compatibility aliases

 

Even though PowerShell provides these compatibility aliases, keep in mind that many of the PowerShell commands work differently than the Cmd.exe command equivalents. Because PowerShell commands are different, you’ll need to learn how to use them. One of the best way to do this is to use PowerShell’s help system.

Using PowerShell’s Help System

Cmd.exe provides help for commands by typing the command name followed by the /? parameter. External executables might or might not support the /? parameter, depending on who wrote the program. In contrast, Microsoft designed PowerShell commands to be discoverable and includes a built-in help system.

There are two main ways to get help for a PowerShell command when you are at the PowerShell command line:

1. Use the Get-Help cmdlet. The Get-Help cmdlet displays information about the command but does not stop after each screenful of information. Typing a command name followed by the -? parameter displays the same information as Get-Help (i.e., Get-ChildItem -? and Get-Help Get-ChildItem are equivalent).

2. Use the Help command. The Help command displays the same information as Get-Help but pauses after each screenful of information using the More command.

If you have PowerShell version 3.0 or newer, you will need to update the help files on your computer before using the Get-Help (or Help) command. See the Update-Help documentation (http://technet.microsoft.com/en-us/library/hh849720.aspx) for more information about updating the help files on your computer.

Let’s start with a simple example. The command help Get-Childitem displays information about the Get-Childitem cmdlet. You can specify the alias name for the command or cmdlet as well. For example, the command help dir displays the same information as help Get-ChildItem because dir is an alias for Get-ChildItem.

The help system can also display more information about a command by specifying additional parameters after the command name. The most common of these parameters are -Detailed and -Full. The -Detailed parameter adds parameter descriptions and examples to the basic help information, and the the -Full parameter displays all of the help information including parameter descriptions and attributes, examples, input and output object types, and additional notes. For example, the command

help Get-ChildItem -Full

will display all help information for the Get-ChildItem cmdlet.

Getting Help in Your Browser

PowerShell is very well documented. Start at the Microsoft.PowerShell.Core Module page (http://technet.microsoft.com/en-us/library/hh847840.aspx). The core “about” topics are all available from this page. The help command in PowerShell displays the same topics as Microsoft’s documentation page, but the documentation page on the web provides a complete list of all topics in an easy-to-access format. In particular, the Core About Topics page contains a wealth of excellent information about a wide variety of PowerShell topics. For example, the about_Pipelines topic explains how the PowerShell pipeline works.

Understanding The Syntax Descriptions

When PowerShell displays help for a command, it provides one or more syntax descriptions in the SYNTAX section. For example, the command help Remove-Item displays the first syntax item as follows:

Remove-Item [-Path] <String[]> [-Credential <PSCredential>] [-Exclude <String[]>] [-Filter <String>] [-Force] [-Include <String[]>] [-Recurse] [-Confirm] [-WhatIf] [-UseTransaction] [<CommonParameters>]

Table 2 explains the notation that the PowerShell help system uses for describing a command’s syntax.

Notation

Meaning

Example

Angle brackets - < >

A data type

<String>

Square brackets - [ ] - surrounding an item

The item is optional

[-Force]

Square bracket pair - [] - after a data type

An array; i.e., one or more items of the named data type

<String[]>

[<CommonParameters>]

One or more optional common parameters (see the about_CommonParameters help topic)

-WhatIf

Table 2: PowerShell syntax notation

 

Running Legacy Command-Line Tools in PowerShell

PowerShell has no problem running legacy command-line tools. PowerShell parameter quoting works differently than in Cmd.exe, but the details I present in my article Running Executables in PowerShell (http://windowsitpro.com/powershell/running-executables-powershell) should help clear up any confusion.

Using the Windows Console

The console-mode version of PowerShell (powershell.exe) uses the Windows console subsystem and thus shares the same basic set of keyboard commands as Cmd.exe. The most common of these are are listed in Table 3.

Keystroke

Description

Up Arrow, Down Arrow

Recalls the previous or next command from the command history.

Left Arrow, Right Arrow

Moves the cursor left or right through the command on the command line.

Ctrl+Left Arrow, Ctrl+Right Arrow

Moves left one word or right one word through the command on the command line.

Home, End

Moves to the beginning or end of the command line.

Page Up, Page Down

Jumps to the first or last command in the command history.

Insert

Toggles insert/overstrike mode.

Ctrl+Home, Ctrl+End

Deletes from the cursor position to the beginning or to the end of the command line.

Esc

Deletes the entire command line.

F7

Displays the command history in a pop-up box. Press Enter to carry out a command, Esc to close the box without carrying out a command, or either the Left Arrow or Right Arrow to copy the command to the command line for editing.

Alt+F7

Deletes all commands from the command history.

Table 3: Common Windows console keyboard commands

 

Switch to PowerShell

PowerShell’s authors designed it for interactive use. The best way to learn PowerShell is to start using it. Update the help and familiarize yourself with how to find information in the help system. Explore the core help topics and experiment with commands. Immerse yourself and you will find that PowerShell will become more intuitive the more you use it. Your time investment will be rewarded with greater productivity.

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish