Skip to main content

ByValue for Generic Objects

This is post 4 of 7 in this series.

Some cmdlets are designed to work with any object.  Take a look at the –InputObject parameter of Sort-Object.

-InputObject []
    Specifies the objects to sort.
   
    When you use the InputObject parameter to submit a collection of items, Sort-Object
    receives one object that represents the collection. Because one object cannot be
    sorted, Sort-Object returns the entire collection unchanged.
   
    To sort objects, pipe them to Sort-Object.
   
    Required?                    false
    Position?                    named
    Default value                none
    Accept pipeline input?       true (ByValue)
    Accept wildcard characters?  false

Whenever you see a parameter accepting objects of type Object or PSObject, then you know that this cmdlet can handle anything. Let’s take a look at the results of Get-SMBShare without using Sort-Object

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

Just by looking at it, the cmdlet sends the objects into the pipeline sorted alphabetically on the Name property.  Now let’s pipe it to Sort-Object.

PS C:\> Get-SmbShare | Sort-Object

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

The results are the same.  Well, that was useful.  The thing that we forgot to do was to tell Sort-Object how to sort our objects.

PS C:\> Get-SmbShare | Sort-Object -Property Description

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


For this cmdlet to do anything, you need to give it some instructions.  In this case, which property to sort on. Sort-Object has been around since PowerShell v1.0.  Get-SMBShare was introduced in PowerShell v3.0, but only if you are running Windows 8 or newer.  In other words, the author of the Sort-Object cmdlet had no idea that the cmdlet Get-SMBShare would ever be created.  The author simple coded the Sort-Object cmdlet to accept all objects. Because of this, we can continue to sort objects evening with PowerShell v5.1.

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.