Stringing PowerShell Commands Together

Piping commands can save quite a lot of time. Another way of looking at piping is stringing items or commands together. In this tutorial, you will learn how to string PowerShell commands together.

Setup

If you have not already done so, click open Windows PowerShell ISE.

Step one.

Usually, in MS-DOS or Windows Command prompt, piping will be ran much like the following code:

Pre dir c:\windows\system32 | find ".exe" Pre

The command performs directory listing of c:\windows\system32 and then pipes the output to the find command to filter for lines that contain the string ".exe". When the same command is ran in PowerShell, we receive an error of course. Keep in mind that various changes have been made that commands such as the one above no longer work in PowerShell, it is implemented in another way.

Recall that DIR is an alias to Get-ChildItem, so by running the code below (one at a time), you will receive the appropriate output in the three formats provided to you per line.

Example

    Get-ChildItem c:\windows\system32 | Format-Table
 
Get-ChildItem c:\windows\system32 | Format-List
 
Get-ChildItem c:\windows\system32 | Format-Wide

    

Copy and Try it

Below is a small glimpse of what each output would appear like by format listing, the listing will come out to be several pages long of course.

Table Format

List Format

Wide Format

As you see, Get-ChildItem continues to return the same exact data every time, regardless in which format it is presented in.

Step two.

Output looking too clean? Of course! Every command that is typed in the console is automatically piped to Out-Default. What is Out-Default? It is a Cmdlet that is in charge of figuring out how to render the output of a given command, if no format is specified of course.

Remarks last but not least…

Every object has a view registered to it. The particular view defines which formatter to use. We discuss output more in depth in another tutorial. Join us next time for additional Windows PowerShell tutorials! Till then…