Overview of Windows PowerShell 5.0

  • 11/2/2015

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 | PowerShell. The Windows PowerShell prompt opens by default at the root of your user folder—for example, C:\Users\Ed.
  2. Enter the command ipconfig /all. This is shown here.

    PS C:\> ipconfig /all
  3. Pipeline 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

Entering a single command into Windows PowerShell is useful, but at times you might 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 enter 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 entered the commands manually, 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.

Running multiple commands

  1. Open Windows PowerShell by choosing Start | Run | PowerShell. The Windows PowerShell prompt opens by default at the root of your user folder.
  2. Enter the ipconfig /all command. Pipeline 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; hostname >>tshoot
    .txt; net statistics workstation >>tshoot.txt