Overview of Windows PowerShell 3.0

  • 2/15/2013

Using command-line utilities

As mentioned earlier, command-line utilities can be used directly within Windows PowerShell. The advantages of using command-line utilities in Windows PowerShell, as opposed to simply running them in the CMD interpreter, are the Windows PowerShell pipelining and formatting features. Additionally, if you have batch files or CMD files that already use existing command-line utilities, you can easily modify them to run within the Windows PowerShell environment. The following procedure illustrates adding ipconfig commands to a text file.

Running ipconfig commands

  1. Start Windows PowerShell by choosing Start | Run | Windows PowerShell. The PowerShell prompt will open by default at the root of your Documents folder.

  2. Enter the command ipconfig /all. This is shown here:

    PS C:\> ipconfig /all
  3. Pipe the result of ipconfig /all to a text file. This is illustrated here:

    PS C:\> ipconfig /all >ipconfig.txt
  4. Open Notepad to view the contents of the text file, as follows:

    PS C:\> notepad ipconfig.txt

Typing a single command into Windows PowerShell is useful, but at times you may need more than one command to provide troubleshooting information or configuration details to assist with setup issues or performance problems. This is where Windows PowerShell really shines. In the past, you would have either had to write a batch file or type the commands manually. This is shown in the TroubleShoot.bat script that follows.

TroubleShoot.bat

ipconfig /all >C:\tshoot.txt
route print >>C:\tshoot.txt
hostname >>C:\tshoot.txt
net statistics workstation >>C:\tshoot.txt

Of course, if you typed the commands manually, then you had to wait for each command to complete before entering the subsequent command. In that case, it was always possible to lose your place in the command sequence, or to have to wait for the result of each command. Windows PowerShell eliminates this problem. You can now enter multiple commands on a single line, and then leave the computer or perform other tasks while the computer produces the output. No batch file needs to be written to achieve this capability.

The following exercise describes how to run multiple commands. The commands used in the procedure are in the RunningMultipleCommands.txt file.

Running multiple commands

  1. Open Windows PowerShell by choosing Start | Run | Windows PowerShell. The PowerShell prompt will open by default at the root of your Documents And Settings folder.

  2. Enter the ipconfig /all command. Pipe the output to a text file called Tshoot.txt by using the redirection arrow (>). This is the result:

    ipconfig /all >tshoot.txt
  3. On the same line, use a semicolon to separate the ipconfig /all command from the route print command. Append the output from the command to a text file called Tshoot.txt by using the redirect-and-append arrow (>>). Here is the command so far:

    ipconfig /all >tshoot.txt; route print >>tshoot.txt
  4. On the same line, use a semicolon to separate the route print command from the hostname command. Append the output from the command to a text file called Tshoot.txt by using the redirect-and-append arrow. The command up to this point is shown here:

    ipconfig /all >tshoot.txt; route print >>tshoot.txt; hostname >>tshoot
    .txt
  5. On the same line, use a semicolon to separate the hostname command from the net statistics workstation command. Append the output from the command to a text file called Tshoot.txt by using the redirect-and-append arrow. The completed command looks like the following:

    ipconfig /all >tshoot.txt; route print >>tshoot.txt; netdiag /q >>tshoot
    .txt; net statistics workstation >>tshoot.txt