PowerShell Pipeline Output


Normally when working with PowerShell you will want to output information to the console. You can do a lot more than that. Also, there is more than one command you can use to send the output to the console.

Pipeline Output

Normally at the end of a pipeline the data is sent to the screen. We could send the output to a variable instead of the screen. There are a few Out verb cmdlets. The default is Out-Default. The Out-Default is going to direct it to the Out-Host. There is Out-GridView that does not work in PowerShell Core. We need to use Windows PowerShell. There is Out-Printer. There is Out-Null which supresses. You can also send the output data to the Windows clipboard by appending the pipe and clip like this: | clip. You can send data to files, just like you send this to files at the command prompt like this: > myfile.txt.

Let’s use grid-view for our list of processes.

get-process | out-gridview

Out_Gridview gives us a window pop up that looks like the following. Click on it to see a larger screenshot.

get-process | out-gridview -PassThru | stop-process

The above command adds the stop-process at the end which adds an OK button at the bottom of the window which when pressed by the user causes the window to disappear because the process is stopped.

Combining the Pipeline with Regular Commands

In Windows PowerShell, we can use clip. That is a Windows executable. We can send the output of a cmdlet to the clipboard. Below is an example of that.

get-process m* | clip

Output to Files

Do you need to send out some text to a file? We have an older post called DOS Command to Append Text to a File that discussed how to do that. First, though we’ll look at sending the output of a cmdlet to a file. For example, to sed the output of get-process to a file called proc.txt, you would write this at the command line: get-process > procs.txt. Alternativly you could run get-process | out-file procs.txt. The file would be created in the current directory. If the file already exists, it will overwrite without warning. If you want to append the output to the existing file would would write this: get-process a* >> procs.txt. If you want to open that file in Notepad to see it, run this: notepad procs.txt.

We can type the contents of the text file to the screen with get-content. At the command line, type get-content and a space and a period and the slash, like this: get-content .\. Then press the Tab key. This will cycle through all of the files in the current folder. When you get to the text file you want to view, press the enter key. Instead of typing get-content you could type cat.

Export

Way back in DOS we were able to do these types of things, however the ability to export is new. We can export out to other formats such as CSV file and XML file. The advantage here is that these formats can be imported into programs such as spreadsheets and databases. We could run get-process | Export-csv c:\test\proc.csv.

We can also export to an XML file. Try get-process | export-clixml procsx.xml.

Import

You can import it back in, but I won’t discuss that here.

Measure-Object

How many processes are there? How large is the data? You can use measure-object this way: get-process | measure-object.