Chapter 2: Basic PowerShell Concepts


Your source for the news, opinions, and reader reaction on the industry's hottest controversy.

In This Chapter

  • Getting Started

  • Understanding the Command-Line Interface (CLI)

  • Understanding Cmdlets

  • Getting Help

  • Understanding Variables

  • Understanding Aliases

  • Creating Your First Script

  • Summary

This chapter brings you up to speed on the technical basics of PowerShell and how to use it, with a focus on the new capabilities offered in PowerShell 2.0 CTP2. You learn how to download and install PowerShell, work with the PowerShell command-line interface (CLI), use cmdlets, access the help features of PowerShell 2.0, and write a basic script. This chapter isn't intended to be a complete getting-started guide; instead, it covers the important concepts you need to understand for later chapters.

Getting Started

The best way to get started with PowerShell is to visit the Windows PowerShell home page, as shown in Figure 2.1.

The URL for the Windows PowerShell home page is

This site is a great resource for information about PowerShell, and enables you to download documentation, tools, read up on the latest product news, and obtain the latest versions of PowerShell. Your next step is downloading and installing PowerShell. Before diving into the installation process, it's important to clarify which version of PowerShell is most appropriate for your requirements. There are currently two versions of PowerShell available for download from Microsoft: PowerShell 1.0 RTW and PowerShell 2.0 CTP2. Each of these versions is described in the following sections.

Figure 2.1

The Microsoft Windows PowerShell Home Page

PowerShell 1.0 RTW

As of this writing, the current released version of PowerShell is version 1.0 RTW (Release To Web). This version of PowerShell was released to the public in November 2006. Although PowerShell 1.0 RTW does not include the new features of PowerShell 2.0 CTP2 discussed in Chapter 1, "Introduction to Shells," it is the most current stable release of PowerShell and, as of this writing, it is the only version that can be recommended for use in a production environment. PowerShell 1.0 RTW can be downloaded for all supported platforms at the following URL:

Enabling PowerShell 1.0 on Windows Server 2008

Windows PowerShell 1.0 is included as a feature of the Windows Server 2008 operating system. However, before PowerShell can be used, it must be added using the ServerManager snap-in. To complete this task, follow these steps:

  1. Log on to the desired server with Local Administrator privileges.

  2. Click Start, and then click Run.

  3. In the Run dialog box, type ServerManager.msc, and then click OK.

  4. In the Features Summary section, click the Add Features task.

  5. On the Select Feature page, select the Windows PowerShell feature, and then click Next.

  6. On the Confirm Installation Selections page, review the selections made, and then click Install.

  7. On the Installation Results page, review the results, and then click Close.

PowerShell 2.0 CTP2

PowerShell 2.0 CTP2 is a Community Technology Preview version of the PowerShell language. This prerelease version of PowerShell 2.0 contains the new language features described in Chapter 1, and is most appropriate for those of us interested in experimenting with the new capabilities of PowerShell 2.0 in a lab or pre-production environment. Prior to installing PowerShell 2.0 CTP2, you need to ensure that your system meets the following minimum installation requirements:

  • Supported operating systems for PowerShell 2.0 CTP2 include Windows XP with Service Pack 2, Windows Vista with Service Pack 1, Windows Server 2003 with Service Pack 1, and Windows Server 2008.

  • The Microsoft .NET Framework 2.0 is required for installation of PowerShell 2.0 CTP2.

  • The Microsoft .NET Framework 3.0 is required to use the new Graphical PowerShell and Out-Gridview cmdlets in PowerShell 2.0 CTP2.

  • The Get-Event cmdlet works only on Windows Vista and Windows Server 2008 and requires the Microsoft .NET Framework 3.5.

  • The Windows Remote Management (WinRM) CTP2 component is required to use the new remoting features in PowerShell 2.0 CTP2.

  • Remoting works only on Windows Vista with Service Pack 1 (SP1) and on Windows Server 2008.

All of the additional components listed can be downloaded from the Microsoft Download Center at (see Figure 2.2).

Before Installing PowerShell 2.0 CTP2

If you currently have PowerShell 1.0 installed on your system, you need to remove it before installing PowerShell 2.0. The steps that follow describe the procedures you need to follow to uninstall PowerShell 1.0, depending on your operating system.

Uninstalling Windows PowerShell 1.0

  • Windows XP-SP2 and Windows Server 2003: Under Add/Remove Programs, select the option to show updates. Remove the PowerShell updates as applicable for your system: KB926139 (en-us), KB926140 (localized), and KB926141 (MUI pack).

  • Windows Vista: Go to Control Panel> Programs and Features> Installed Updates. Uninstall the PowerShell update: KB928439.

  • Windows Server 2008: In Windows Server 2008, PowerShell 1.0 comes as an optional component. If you have enabled PowerShell 1.0, you must turn the feature off before you can install PowerShell 2.0 CTP2. Launch Server Manager and choose the option to remove features. Select PowerShell from the list and disable the features.

Note - Please note that you will not be able to install PowerShell 2.0 on a Windows 2008 server that has Exchange 2007 or the Exchange Management Shell installed.

Figure 2.2

The Microsoft Download Center

Install and Configure WinRM (Windows Remote Management)

WinRM is Microsoft's implementation of the WS-Management Protocol, which is a Simple Object Access Protocol (SOAP)-based protocol that provides a common method for systems to access and exchange management information. By default, WinRM uses ports 80 and 443 for its transport; thus, it is compatible with most firewalls. However, WinRM also enables these default ports to be changed if necessary. While WinRM is natively supported in Windows Vista and Windows Server 2008, the WS-Management 2.0 CTP component is required to support remoting in the PowerShell 2.0 CTP2 release. The WS-Management 2.0 CTP update for can be downloaded from:

Note - For Windows Vista systems, Windows Vista Service Pack 1 is required to enable WinRM functionality.

Downloading and Installing PowerShell 2.0

After installing .NET Framework 2.0 and .NET Framework 3.0, and after verifying that any necessary WinRM components are installed on your system, your next step is to download the PowerShell 2.0 CTP2 installation package from (see Figure 2.3).

Figure 2.3

Download Windows PowerShell 2.0

To install PowerShell, on the download page, find the correct PowerShell installation package for your x86 or x64 version of Windows. Then, download the PowerShell installation package by clicking the appropriate download link. Next, start the PowerShell installation by clicking Open in the download box or double-clicking the installation file. (The filename differs depending on the platform, Windows version, and language pack.) After the installer has started, follow the installation instructions.

Another installation method is a silent installation at the command line, using the /quiet switch with the PowerShell installation filename. This installation method can be useful if you plan to install PowerShell on many different systems and want to distribute the installation via a logon script, Systems Management Server (SMS), or another software management method. To perform a silent installation, follow these steps:

  1. Click Start > Run.

  2. Type cmd, and then click OK to open a cmd command prompt.

  3. Type PowerShell-exe-filename /quiet (replacing the italicized text with the PowerShell installation filename) and press Enter.

After installing PowerShell, you can access it through three different methods. To use the first method of accessing it from the Start menu, follow these steps:

  1. Click Start > All Programs > Windows PowerShell 2.0.

  2. Click Windows PowerShell.

To use the second method, follow these steps:

  1. Click Start > Run.

  2. Type PowerShell in the Run dialog box, and then click OK.

Both these methods open the PowerShell console, which is shown in Figure 2.4.

Figure 2.4

The PowerShell console

Follow these steps to use the third method from a cmd command prompt:

  1. Click Start > Run.

  2. Type cmd and click OK to open a cmd command prompt.

  3. At the command prompt, type powershell, as shown in Figure 2.5, and press Enter.

Figure 2.5

The PowerShell console launched through the cmd command prompt

Configure WSMan Settings

As mentioned earlier, the PowerShell Remoting and PSJobs features rely on Windows Remote Management (WinRM) technology. For these features to work, a number of configuration changes need to be made to the PowerShell 2.0's default WSMan settings. Fortunately, this process is made simple through a PowerShell script called Configure-Wsman.ps1 in the $pshome folder. This script configures WS-Man settings. The following command executes the Configure-WSMan.ps1 script from the $pshome location:

& $pshome\Configure-Wsman.ps1

Please note: Due to PowerShell 2.0's default script execution policy (initially set to Restricted), the Configure-WSman.ps1 script will initially not be allowed to run. The screen shot that follows shows the error that you receive if you attempt to run this script without modifying the default script execution policy.

PS C:\> & $pshome\Configure-Wsman.ps1

File C:\WINDOWS\system32\WindowsPowerShell\v1.0\\Configure-Wsman.ps1

cannot be loaded because the execution of scripts is disabled on this

system. Please see "get-help about_signing" for more details.
At line:1 char:2+ & <<<< $pshome\Configure-Wsman.ps1PS C:\>

To allow the Configure-WSMan.ps1 script to execute successfully, you can use the Set-ExecutionPolicy cmdlet to set the execution policy to RemoteSigned, as shown in the example that follows. After the script completes, you can optionally use the Set-ExecutionPolicy cmdlet to return PowerShell to its default script execution policy. The command sequence that follows shows three separate actions: setting the script execution policy to RemoteSigned, executing the Configure-WSMan.ps1 script, then setting the script execution policy back to Default. (We use the Set-ExecutionPolicy cmdlet again later in this chapter when writing a basic PowerShell script. Chapter 5, "Understanding PowerShell Security," discusses PowerShell security and related best practices in more detail.) The screen shot that follows shows an abbreviated version of the verbose output generated by running the Configure-WSMan.ps1 script.

PS C:\> set-executionpolicy RemoteSignedPS C:\> & $pshome\Configure-Wsman.ps1Configuring WSMan...Configuring WSMan CompleteOpening port 80 and port 443PS C:\> set-executionpolicy DefaultPS C:\>

After you have completed the steps described previously, PowerShell 2.0 CTP2 is configured and ready for use.

Understanding the Command-Line Interface (CLI)

The syntax for using PowerShell from the CLI is similar to the syntax for other CLI shells. The fundamental component of a PowerShell command is, of course, the name of the command to be executed. In addition, the command can be made more specific by using parameters and arguments for parameters. Therefore, a PowerShell command can have the following formats:

[command name][command name] -[parameter][command name] -[parameter] –[parameter] [argument1][command name] -[parameter] –[parameter] [argument1],[argument2]

Note - In PowerShell, a parameter is a variable that can be accepted by a command, script, or function. An argument is a value assigned to a parameter. Although these terms are often used interchangeably, remembering these definitions is helpful when discussing their uses in PowerShell.

You can see an example of using a command, a parameter, and an argument by running the dir command with the /w parameter (which displays the output of dir in a wide format) and an argument of C:\temp\*.txt, as shown here:

[C:\>dir /w C:\temp*.txt Volume in drive C is OS Volume Serial Number is 1784-ADF9 Directory of C:\tempBad Stuff.txt mediapc.txt  note.txt  Progress.txt    4 File(s)   953 bytes    0 Dir(s) 16,789,958,656 bytes freeC:\>

The result of this command is a wide-format directory listing of all the .txt files in C:\temp. If you use the dir command without any parameters or arguments, the outcome is entirely different. The same result happens with PowerShell. For example, here is a basic PowerShell command that gets process information about explorer.exe:

PS C:\> get-process -Name explorerHandles NPM(K) PM(K)  WS(K) VM(M) CPU(s)  Id ProcessName------- ------ -----  ----- ----- ------  -- ----------- 807    20     31672  14068 149   62.95   1280 explorerPS C:\>
1 2 3 4 Page 1
Page 1 of 4
The 10 most powerful companies in enterprise networking 2022