DAY-4: PowerShell Pipeline and Exporting Data


Pipeline - Connecting commands 

PowerShell connects commands to each other using something called a Pipeline. The pipeline provides a way for one command to pass, or pipe, its output to another command, allowing that second command to have something to work with.

The concept of joining commands is not limited to PowerShell, but PowerShell takes the same piping to a greater extend for better effect. 

Exporting Output to Files

1. Get-Process
2. Get-service

When we run above commands, a table with several columns of information appears on the screen. It's great to have information on the screen, but that isn't all you might want to do with the information. For example, if you want to export the information into a CSV file that could be read into an application like Microsoft excel. 

Exporting to CSV

Exporting to a file is where the pipeline and a second command come in handy.

Get-Process | Export-csv C:\procs.csv

We have piped objects that resulted from Get-Process, into a CSV file. Now if we open the file, procs.csv in notepad, we will see the content in the file like as we are seeing in below snapshot.







           










The first line of the file will be a comment, preceded by a # character, and it is identified, the kind of information that's included in the file. In the above file, it's System.Diagnostics.Processwhich is understood name that windows use to identify the information regarding the running process. The second line will be column headings, and subsequent lines will list the information for the various processes running on the computer.

You can pipe the output of any Get cmdlet to Export-CSV and get an excellent result. You may also notice that the CSV file contains more information that what's normally shown on screen. That's deliberate. The shell knows it couldn't possibly fit all of that information on the screen, so it uses a configuration file, supplied by Microsoft, to select the most important information for on-screen display.

What if CSV files aren't what you need? PowerShell also has an Export-CliXML cmdlet, which creates a generic command-line interface(CLI) Extensible Markup Language (XML) file. CliXML unique to PowerShell, but any program capable of understanding XML can read it.

Passing data to a text File

Whenever you have nicely formatted output, like the tables generated by Get-Service or Get-process, you may want to preserve that in a file, or even on paper. Normally, cmdlets output the data to screen, which  PowerShell refer to as Host, but you can change where the output goes.

Get-childitem | Out-file C:\directory.txt

Out-File is a Cmdlet, that let you put the output of any command in a text file. By using Get-Help cmdlet, you can get the full details of the command. 

Powershell has a variety of Out- Cmdlets, that let you Output the result of piped command in the way you want. You can check the below link for getting more information on Out- Commands.


Converting to HTML

Sometimes, we want to produce HTML reports. Passing data to a text file or exporting to a CSV file will not help. Now how can we find, that PowerShell converts the data into HTML or not? In this case, Get-Help can help us finding commands that include HTML. From Get-Help, we have got the command which can convert the data from Cmdlets to HTML. ConverTo-HTML, this command produces well formatted, generic HTML that will display in any web browser.

Get-service | ConvertTo-HTML

Notice that ConvertTo-HTML doesn't require a filename, so how we can pull the converted data into a text file. Think a bit, if we can add one pipeline, why would not we add another pipeline to out the converted data into a file.

Get-service | ConvertTo-HTML | Out-file C:\process.html

See if this works for you as well.

Understanding What Pipeline Do

You can pipe as much as you can, but remember one thing, except for the last Cmdlet, every Cmdlet should produce an output which can be piped into the other. For example,

Get-service | out-file C:\ser.txt | out-file F:\ser.txt

Now in above example, I have piped another OUT cmdlet. Get-service will produce an output, this output carries forward to out-file cmdlet which directs the output to C drive in SER.txt file. But, Out-file doesn't produce any output, so after running the command you will see a text file in F drive with name SER.txt, but with no content in it. This mean out-file F:\ser.txt did not receive any data from the pipeline and produces a text file with no content. So while piping Cmdlets, you should take care that which Cmdlets produce output and which doesn't.

Now you have the idea, how things work in PowerShell Pipeline. Also, we know how to export or out the data from the PowerShell console to files. Now I will be introducing a LAB section in my each Blog. It will include few problems to think, based on the things I have described in my blog. You can comment if you have any query regarding the topic explained in this blog.

LAB

1, What happens if you run Get-Service | Export-CSV services.csv | out-file  from your console?

2. Export-CSV modifies the system because it can create and modify files. What parameter would    
    prevent them from overwriting an existing file?

3. Is there a way to eliminate the # comment line from the top of an exported-csv file?

In next blog, we will be covering below listed topics:
1. Adding Commands - Modules & PSSnapin
2. Playing With Modules

Comments

Popular posts from this blog

DAY-1 : POWERSHELL…WHAT IT IS>>>AND WHY

DAY-7: POWERSHELL OBJECT'S

Day-5: PowerShell Modules & PSSnapin