Windows Vista
Windows 7
Windows Azure
Windows Server
Windows Phone
Windows 7

Using Windows PowerShell and the PowerShell ISE (part 1)

5/20/2011 6:02:47 PM
Windows PowerShell is an extensible version of the command prompt. It is integrated with the Microsoft .NET Framework, which gives it extensive capabilities well beyond the command prompt. Windows 7 comes with Windows PowerShell 2.0 installed (the same version that's installed on Windows Server 2008 R2).

The PowerShell commands can be used to perform and automate many administrative tasks such as managing services, managing event logs, modifying the Registry, and interacting with Windows Management Instrumentation. PowerShell was designed with scripting in mind, so you'll find that you can create elegant scripts that can automate many of your administrative tasks.

One of the challenges with PowerShell is that it is so rich in capabilities and features that it can be intimidating. However, you can start using it without understanding everything about it. You can learn as you go.

You'll get the most out of this section if you're able to launch PowerShell, execute the commands, and see them in action. You can launch PowerShell normally or with administrative permissions. To launch it with administrative permissions, you'd right-click and select Run As Administrator. However, I strongly recommend that you launch it normally unless you need specifically to use administrative permissions (such as when you need to change the execution policy).

Just as the command prompt has its own environment, PowerShell too has its own environment. It also has a distinctive look and feel. Launch it and see for yourself by clicking Start => All Programs => Accessories => Windows PowerShell => Windows PowerShell.

While the look and feel are a little different, you can right-click the title bar and have access to the same menu as the command prompt. You can also copy and paste to and from the PowerShell window and enable QuickEdit mode, so this is a little easier.

In addition to the Windows PowerShell, you'll see the Windows PowerShell ISE, which is the Integrated Scripting Environment (ISE). If you're running a 64-bit system, you'll see the following four choices:

  • Windows PowerShell (x86)

  • Windows PowerShell ISE (x86)

  • Windows PowerShell

  • Windows PowerShell IS

I haven't run across any issues using the 64-bit Windows PowerShell and ISE for all my scripts, but if you need to step down to the 32-bit version, you can.

1. Windows PowerShell ISE

The Windows PowerShell Integrated Scripting Environment can be used just as easily as the Windows PowerShell prompt, but it gives you a lot more capabilities. Figure 3.2 shows the PowerShell ISE.

Figure 1. Windows PowerShell ISE

You can see that it has a familiar menu across the top just like any other Windows application. Below the menu are several icons. As with other applications, you can hover your mouse over the icon to see the name. The most common icons you'll use are Save, Run Script, and Run Selection. The Run Script icon is the right-arrow icon, and if you highlight any text, the icon next to it will be enabled so that you execute the text that you've highlighted.

The middle pane shows the output of the script. The bottom pane is the PowerShell prompt. Any commands you can enter at the PowerShell prompt can also be entered at the prompt in the bottom pane. You can even save the script you're writing and execute it using the full path and name from this prompt.

At the bottom right, you'll notice some status that can be very useful. Right now, I have the cursor at the beginning of line 5, and the status shows it at "Ln5 Col 1." Sometimes, an error message will indicate there's an error at "line 43, col 17." Using the ISE, it's easy to determine exactly what the error refers to by using this status information.

Last, you can easily zoom in to make the text bigger or smaller. Currently, it's set at 13, but you can move the pointer at the bottom of the screen to the right to increase the size or to the left to decrease the size. As you play around with PowerShell, feel free to use either the PowerShell interface or the PowerShell ISE.

2. PowerShell Commands

PowerShell includes three types of commands that can be executed or scripted:

Cmdlets PowerShell cmdlets are built-in commands. They are different from command-prompt commands in that they are tightly integrated with Microsoft's .NET Framework, providing a much richer set of capabilities. You can think of them as mini-programs. Many cmdlets can accept parameters and return values. Values can be displayed, assigned to variables, or passed to other cmdlets or functions.

As an example, the Get-Command cmdlet will retrieve a list of all PowerShell commands. You can modify the command with parameters to alter the results. For example, the following three commands can be used to retrieve a list of only the cmdlets, only the aliases, and only the functions. In these examples, the Get-Command cmdlet is being used with the -CommandType switch, and cmdlet, alias, and function are the parameters passed to the Get-Command cmdlet.

Get-Command -CommandType cmdlet
Get-Command -CommandType alias
Get-Command -CommandType function

Aliases An alias is another name for a cmdlet. Many command-prompt commands have been rewritten as PowerShell commands. While the actual PowerShell command is different from the command-prompt command, many aliases have been created so that you enter the command-prompt command and it will launch the PowerShell command.

As an example, the CD command-prompt command is used to change the current directory. The PowerShell cmdlet is Set-Location, but CD is recognized as an alias for Set-Location.

Try it. These two commands achieve the same result of changing the current path to the root of the C: drive:

CD \
Set-Location \

All command-prompt commands have not been rewritten. If you enter a command such as path, which works at the command prompt, you'll see it doesn't work here. You can enter the Get-Alias command to get a list of all the aliases supported within PowerShell.

In addition, some longer cmdlets have been rewritten as aliases. As an example, the Get-WMIObject cmdlet has an alias of gwmi.

Functions A function is a type of command that you can run within PowerShell or PowerShell scripts. They are very similar to cmdlets. They can accept parameters and can return values that are displayed, assigned to variables, or passed to other functions or cmdlets.

A commonly used function is the Help function, which provides help on PowerShell topics and concepts. When executed without a parameter, it provides one set of information. When a valid PowerShell command is added as a parameter (such as Help Get-Command), it provides specific help on the command. Another function that is commonly used is the drive letter (such as C:, D:, and so on). It will change the current PowerShell prompt location to the named drive. You can also create your own functions.

Any of these commands can be executed from the PowerShell prompt or embedded into PowerShell scripts. Also, many server applications (such as Internet Information Services (IIS), Exchange Server 2007, and Exchange Server 2010) are heavily intertwined with PowerShell commands. In other words, what you learn here on Windows 7 will be useful when you're managing servers.

3. Verbs and Nouns

PowerShell cmdlets are composed of verbs and nouns in the format of verb-noun with the dash (-) separating the two. You may remember from your English classes that verbs denote action and nouns are things. Common PowerShell verbs are Get, Set, and Test. You can combine them with nouns to get information on objects or to set properties on objects.

Earlier, you saw the Set-Location cmdlet that is similar to the command-prompt change directory command (CD). Set is the verb and Location is the noun. Similarly, Get-Service uses the verb Get to retrieve information on services, and Out-File uses the verb Out to send information to a file.

If you can remember Get, Set, and Test, you're halfway there because PowerShell will give you a lot of clues. Try this. Type in Get- and then press the Tab key. PowerShell will display each of the legal commands that can be executed starting with Get- (from Get-ACL to Get-WSMan-Instance).

You can do the same thing with the Set and Test verbs. Type in Set- and press Tab to see all of the objects (nouns) that can have properties set: Set-ACL to Set-ManQuickConfig. If you type in Test-, you'll be able to tab through the choices from Test-ComputerSecureChannel through Test-WSMan.

Functions don't necessarily follow the verb-noun format but instead are just commands. For example, the E: function will set the current drive to E: by actually calling the Set-Location cmdlet using the parameter E:. However, the Clear-Host function does use a verb-noun format to indicate the host screen (the noun) is being cleared (the verb).

4. Sending Output to a Text File

Many times you'll want the output of the PowerShell command to be written to a file instead of the screen. This can be especially useful when you're creating documentation. While learning, you may want to send the output of some of these commands so you can read them later. There are two ways to do this.

The first is the same method used with the command prompt using the redirection symbol (>). For example, if you want to send a list of all aliases to a text file named PSAlias.txt and then open the text file, you could use these commands:

Get-Alias > PSAlias.txt
Notepad PSAlias.txt

PowerShell also uses the Out-File cmdlet to send the output to a file. If you wanted to send a listing of services and their current status to a file, you could use the Get-Service cmdlet and the Out-File cmdlet in the same line.

Just as pipelining can be used at the command line, it can be used in PowerShell. When you want the output of one PowerShell command to be used as the input to another command on the same line, you would separate the commands with the pipe symbol (|), which is typically Shift+backslash (\) on most keyboards. The following shows how this is done:

Get-Service | Out-File Service.txt
Notepad Service.txt

Of course, if you wanted to save the file to a different location, you could include the full path. For example, you could save the file in the Data folder on the C: drive with this command:

Get-Service | Out-File C:\Data\Service.txt

5. PowerShell Syntax

Many of the rules that apply to the command prompt also apply to PowerShell. As a reminder, here are some of these rules:

  • Spelling counts.

  • Commands are not case sensitive.

  • Commands are modified with switches.

  • Spaces usually need to be enclosed in quotes.

  • Help is always available.

  • DOSKEY saves typing.

Although these items are the same, there are a couple of other items you should know about PowerShell. These include variables, comparison operators, and command separators (such as parentheses, brackets, and braces) which are all covered in the following sections.

5.1. Variables Created with a $ Symbol

Many times, you'll need to create a variable that will be used to store information and later retrieve information. This can be as simple as loading a number (such as 5) into a variable, like this:

$num = 5

Variables can be used to create a pointer to an object. For example, the following variable is used to store a pointer to an instance of the calculator program (named calc.exe):

$calc = get-wmiobject -query

"select * from win32_process where name = 'calc.exe'"


Even though the previous code is shown on two lines, it would be entered on a single line.

You can also use variables to hold collections of information. Collections can hold several similar items. As an example, the following command will load a list of all the event logs into the variable named $Log and store them as a collection:

$Log = Get-EventLog -list

Once the collection is created, data about any of the items stored in the collection can be retrieved.

5.2. Comparison Operators

You can use many comparison operators to specify or identify a condition. Comparison operators compare two values to determine whether a condition exists. Most comparison operators return True if the condition exists and a False if it doesn't exist.

As a simple example, imagine a variable called $num had a value of 100. The comparison $num -eq 100 would be evaluated as True, while $num -eq 5 would be evaluated as False.

Comparisons don't have to be numbers. They can also be text; however, when comparing text data, the text string needs to be enclosed in quotes. For example, if you wanted to know if a variable named $str has a value of "MCITP," you'd use this comparison:

$str -eq "MCITP"

String comparisons are case insensitive by default. In other words, both of these would evaluate to True:

$str -eq "mcitp"
$str -eq "MCITP"

However, if you want the comparison to be case sensitive, you can add the letter c to the operator switch, like this:

$str -ceq "True"

Table 1 lists some of the commonly used comparison operators. You can enter these at the command line to see the result. For example, to see how the -eq command works, you could populate the variable $num with 5 and then use the variable in a comparison like this:

$num = 5
$num -eq 100

PowerShell will return False.

Table 1. Comparison operators
-eqEquals, as in $x -eq 100 or $x -eq "y"
-neNot equal, as in $x -ne 100 or $x -ne "y"
-gtGreater than, as in $x -gt 100
-geGreater than or equal to, as in $x -ge 100
-ltLess than, as in $x -lt 100
-leLess than or equal to, as in $x -le 100
-likeCompares strings using the wildcard character * and returns True if a match is found. The wildcard character can be used at the beginning, middle, or end to look for specific matches. If a variable named $str holds the string "MCITP Windows 7," then all of the following comparisons will return True:
$str -like "MCITP*"
$str -like "*Win*"
$str -like "*7"

-notlikeCompares strings using the wildcard character * and returns True if the match is not found.

You'll see comparison operators used in many different ways. For example, they are used in cmdlet switches when a comparison is needed and in IF statements when you're scripting.

5.3. Parentheses, Brackets, and Braces

PowerShell commands can include parentheses (( )), brackets ([ ]), and braces ({ }). Braces are also referred to as "curly brackets" and even "funky brackets" by some. Each is interpreted differently within PowerShell.

Parentheses ( ) Parentheses are commonly used to provide arguments. For example, when a script needs to accept a parameter, you can use Param($input). Here the $input is identified as an argument for the Param command.


The terms parameter and argument are often used interchangeably. On one level, they are the same thing; but there is a subtle difference between the two. A parameter is provided as input to a piece of code, and an argument is what is provided. The value is the same but the perspective is different. A parameter is passed in, and the value of this parameter is used as an argument within the code.

Brackets [ ] Brackets are used for various purposes. You'll see them used when accessing arrays, when using -like comparisons, and with some parameters. As an example, the following command uses them to indicate the output should include only names that start with a letter:

Get-wmiObject Win32_ComputerSystem | Format-List [a-z]*

Braces { } Braces are used to enclose a portion of code within a statement that is interpreted as a block of code. You'll see them in condition statements. The following example shows how braces are used to separate the block in the Where clause in a single line of code:

Get-service | Select * | Where {$_.name -eq $Name}

The point here is not that you need to memorize when these characters need to be used. Instead, the goal is to let you know that all three could be used and, when you're executing code and writing scripts, to recognize each. If you replace curly braces with parentheses when you type in your own code, you'll find things simply don't work.

Other -----------------
- Using the Windows Command Prompt (part 4)
- Using the Windows Command Prompt (part 3) - A Sampling of Commands
- Using the Windows Command Prompt (part 2)
- Using the Windows Command Prompt (part 1)
- Automating the Deployment of Windows 7 : Using the Microsoft Deployment Toolkit 2010 (part 2) - Creating a Task Sequence
- Automating the Deployment of Windows 7 : Using the Microsoft Deployment Toolkit 2010 (part 1) - Installing MDT 2010 & Creating a MDT 2010 Deployment Share
- Deploying Images with Windows Deployment Services (part 3) - Capturing Images with WDS
- Deploying Images with Windows Deployment Services (part 2) - Adding and Configuring WDS
- Deploying Images with Windows Deployment Services (part 1) - WDS Requirements & Deploying Images with WDS
- Automating the Deployment of Windows 7 : Imaging with the Windows Automated Installation Kit (part 3) - Preinstallation Environment & System Image Manager
Top 10 video Game
-   Heroes Charge - Trio VW + EB + SIL vs WC FD CW EB Com
-   PlanetSide 2 | PS4 Launch Trailer 'Redefining Massive Warfare
-   DRIVECLUB | PS4 All-Action Trailer
-   Wave of Darkness [PC] Early Access Trailer
-   Tony Hawk's Pro Skater 5 | "THPS is Back" Trailer
-   Poly Bridge [PC] Trailer
-   Persona 5 | Leaked Trailer 2 w/ Dancing All Night
-   Evolve [PS4/XOne/PC] Lennox Gameplay Trailer
-   Lexus Slide | Real, rideable hoverboard
-   Gears of War: Ultimate Edition | Teaser Video
-   Rise of Incarnates [PC] Zeus Trailer
-   Heroes Reborn | The Extraordinary Among Us (Preview)
-   Battleborn | E3 2015 Gameplay Demo
-   Fortnite [PC] Mac Showcase Trailer
-   Overwatch [PC] Zarya Gameplay Trailer
Popular tags
Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 windows Phone 7 windows Phone 8
programming4us programming4us
Popular keywords
HOW TO Swimlane in Visio Visio sort key Pen and Touch Creating groups in Windows Server Raid in Windows Server Exchange 2010 maintenance Exchange server mail enabled groups Debugging Tools Collaborating
programming4us programming4us
PS4 game trailer XBox One game trailer
WiiU game trailer 3ds game trailer
Trailer game
Heroes Charge
Windows Vista
Windows 7
Windows Azure
Windows Server
Game Trailer