Skip to main content

The 30,000 Foot View of the PowerShell Pipeline

This is post 2 of 7 in this series.

In simple terms, this is what occurs when you use the PowerShell pipeline. You execute a cmdlet that recovers a specific type of object.  The cmdlet then places one object into the PowerShell pipeline.  This object passes to the next cmdlet in the pipeline.  After that cmdlet is finished with it, it moves to the next cmdlet and so on and so forth.  Once the object has exited the pipeline, the first cmdlet releases the next object into the pipeline.  The important thing to note at this time is that only one object is processed in the pipeline at any one time.

Let’s take a look at a simple example.
PS C:\> Get-SmbShare

Name   ScopeName Path                              Description   
----   --------- ----                              -----------   
ADMIN$ *         C:\WINDOWS                        Remote Admin  
C$     *         C:\                               Default share 
D$     *         D:\                               Default share 
E$     *         E:\                               Default share 
IPC$   *                                           Remote IPC    
print$ *         C:\WINDOWS\system32\spool\drivers Printer Drivers

Six SMB Share objects are presented. Let’s pass these six objects to Where-Object and only keep any share whose Description property is equal to “Default Share”.

PS C:\> Get-SmbShare | Where-Object Description  -eq "Default Share"

Name ScopeName Path Description 
---- --------- ---- ----------- 
C$   *         C:\  Default share
D$   *         D:\  Default share
E$   *         E:\  Default share

As you can see, we only have 3 objects left to work with.  Here is the process for the first few objects.
  1. Get-SMBShare gathered 6 SMB Share objects and placed the first object into the PowerShell Pipeline.  The first object was the SMB Share named Admin$.
  2. The object was passed to the cmdlet Where-Object for processing.
  3. Where object looked at the value of the Description field for this object. The object has a value of “Remote Admin”.
  4. Since this description does not meet our criteria of “Default Share” the object is removed from the pipeline and discarded from memory.
  5. Where-Object signals to Get-SMBShare to send the next object if it has any more.
  6. Get-SMBShare release the next object that has an SMB Share name of C$.
  7. Where-Object receives the object and evaluates the Description property’s value.
  8. Since the value of the Description matches our criteria of “Default Share” The object is put back into the PowerShell pipeline.
  9. Since there are no more cmdlets in the pipeline, the object is sent to the default output device (your monitor). Actually it passes through a complex formatting system, put that is a discussion for another time.
  10. The pipeline signals to the first cmdlet that the pipeline is clear and to send another object if it has one.

T


All of that just by sticking the “|” character between two cmdlets.  The output of the cmdlet on the left becomes the only input for the cmdlet on the right.

Comments

Popular posts from this blog

Adding a Comment to a GPO with PowerShell

As I'm writing this article, I'm also writing a customization for a PowerShell course I'm teaching next week in Phoenix.  This customization deals with Group Policy and PowerShell.  For those of you who attend my classes may already know this, but I sit their and try to ask the questions to myself that others may ask as I present the material.  I finished up my customization a few hours ago and then I realized that I did not add in how to put a comment on a GPO.  This is a feature that many Group Policy Administrators may not be aware of. This past summer I attended a presentation at TechEd on Group Policy.  One organization in the crowd had over 5,000 Group Policies.  In an environment like that, the comment section can be priceless.  I always like to write in the comment section why I created the policy so I know its purpose next week after I've completed 50 other tasks and can't remember what I did 5 minutes ago. In the Group Policy module for PowerShell V3, th

Return duplicate values from a collection with PowerShell

If you have a collection of objects and you want to remove any duplicate items, it is fairly simple. # Create a collection with duplicate values $Set1 = 1 , 1 , 2 , 2 , 3 , 4 , 5 , 6 , 7 , 1 , 2   # Remove the duplicate values. $Set1 | Select-Object -Unique 1 2 3 4 5 6 7 What if you want only the duplicate values and nothing else? # Create a collection with duplicate values $Set1 = 1 , 1 , 2 , 2 , 3 , 4 , 5 , 6 , 7 , 1 , 2   #Create a second collection with duplicate values removed. $Set2 = $Set1 | Select-Object -Unique   # Return only the duplicate values. ( Compare-Object -ReferenceObject $Set2 -DifferenceObject $Set1 ) . InputObject | Select-Object – Unique 1 2 This works with objects as well as numbers.  The first command creates a collection with 2 duplicates of both 1 and 2.   The second command creates another collection with the duplicates filtered out.  The Compare-Object cmdlet will first find items that are diffe

How to list all the AD LDS instances on a server

AD LDS allows you to provide directory services to applications that are free of the confines of Active Directory.  To list all the AD LDS instances on a server, follow this procedure: Log into the server in question Open a command prompt. Type dsdbutil and press Enter Type List Instances and press Enter . You will receive a list of the instance name, both the LDAP and SSL port numbers, the location of the database, and its status.