PowerShell tutorial part 2.

This weeks PowerShell tutorial covers how to create pipelines and output management. Powershell pipelines are a series of cmdlets that pass objects from one to another. Each cmdlet generates an object and passes it to the next cmdlet via the pipeline. The receiving cmdlet uses that object as input and generates its own object as output. Connecting pipelines is done by using the pipe character (|).

Take for example the Get-Service cmdlet. When we run this we get a list of all services running and stopped. But lets say we only want to view the running services. For this we use a pipeline.

Get-service | Where-Object {$_.status -eq 'running}

The pipe operator connects the Get-Service cmdlet to the Where-Object cmdlet. The output from Get-Service is passed to Where-Object. The Where-Object now uses the output from Get-Service as input to filter the information based on the Status property value. If the expression enclosed in brackets {} is true then the Where-Object passes that object down the pipeline and filters out the other objects. In this example the Where-Object expression says that the status property must be equal to “running”. To access a property in the pipeline in this manner, we use the $_ variable. To set the $_.status variable we used -eq (equal to) to the string ‘running’.

Now lets assume that this is more information then we need and all we really want are the names of the running services without any other details. For this we would use;

Get-Service | where {$_.status -eq 'running'} | select displayname

As before the Where-Object cmdlet has been replaced with it’s alias “where”. And the Select-Object which has been aliased by “select” is filtering the output from Where-Object (where) to filter the output so that all we see are the displayed names of the running services.

Now lets take a look at some of the formatting options we have available to us. We’ll start with;

Get-process powershell

When you run this command it is displayed in the default format. However there are other formats of which we can choose from. Powershell supports four cmdlets that format output:

* Format-table displays data in a table and is also the default format
* Format-list displays data in a list
* Format-wide displays data in a wide table but only one property value for each item
* Format-custom displays data in a custom format which uses configuration information stored in a .ps1xml file. You can use the Update-FormatData cmdlet to update a format files.

We can change the format of the output using the previous command by piping Format-list.

Get-Process powershell | format-list

Now your results should be in list form.

Powershell applies the default format to an output and sends that output to the console window unless you use one of the four format cmdlets. You can however control where to send that output.

* Out-Host sends output to the console and is the default option.
* Out-Default sends output to the default formatting cmdlet.
* Out-File sends output to a specified file.
* Out-Null deletes output and doesn’t send it to the PowerShell console.
* Out-Printer sends output to a printer
* Out-String converts the pipeline object to an array of strings

To control a statement’s output pipe the output cmdlet at the end of the pipeline.

Get-process powershell | Format-List | Out-File c:sysinfopstest.txt

you can also append output to a file by typing;

Get-process powershell | Format-List | Out-File c:sysinfopstest.txt -append

If you would like to sort the results in descending order instead of the default ascending order you can do so by using a sort command;

dir c:windows | where {$_.Length -gt 400000 | sort -property Length -descending

this statement aliases the Get-ChildItem cmdlet with dir and passes that to the Where-Object cmdlet (aliased as where). The Where-Object cmdlet specifies that the length must be greater then 400,000 bytes. Then the results are sorted by the Sort-Object cmdlet (aliases as sort) in descending order as specified by -descending.

One thought on “PowerShell tutorial part 2.
  1. I’ve been noticing that the pipe is becoming quite popular to provide structure to page titles and the like in general on the internet these days. The language that we use to instruct humans seem to work in a remarkably similar fashion to machine code. I was just sitting here musing on that as I was reading this post.

    Have a fabulous week everybody!

Leave a Reply