Prof. Powershell

One-of-a-Kind PowerShell Cmdlets

PowerShell can help you retrieve collections of objects or services, or just a unique subset of information. Let's look at a few cmdlets that can help you narrow your focus.

In PowerShell we're often working with collections of objects such as processes or services. But there may be scenarios where we want a unique data set. Sometimes we want a unique set of objects and other times a unique set of property names. There are a few ways you can gather this type of information.

The Get-Unique cmdlet is the first place you might start. The cmdlet has an alias of unique. One important note is that you must sort items before piping them to Get-Unique or you won't get correct results. Another point to remember is that unique items are case sensitive. I find this cmdlet works best when you are trying to get unique objects:

PS C:\> 1,3,1,5,6,3,3,5,6 | sort | unique

You can also have the cmdlet return a single object for each type:

PS C:\> 1,3,"a","b" | sort | get-unique -OnType

Personally, I don't use this cmdlet often. Usually I'm more interested in unique properties.

We can use Sort-Object to also return a unique data set based on a property name:

PS C:\> get-process | sort Company -unique | select Name,Company

Name                  Company
----                  -------
hpwuschd2             Hewlett-Packard
IAANTmon              Intel Corporation
Energy Management     Lenovo (Beijing) Limited
utility               Lenovo(beijing) Limited
TpShocks              Lenovo.
svchost               Microsoft Corporation
RtHDVBg               Realtek Semiconductor
SugarSyncManager      SugarSync, Inc.
SynTPEnh              Synaptics Incorporated
Jing                  TechSmith Corporation

Sort only returns a unique subset of objects based on the sorted property, in this example that is the Company property of the process object. When using this cmdlet, the sort is case insensitive by default so the unique names are also case insensitive. Of course you can use the cmdlet's -CaseSensitive parameter to change this behavior:

PS C:\> get-wmiobject win32_service | sort Startname -unique -CaseSensitive | select StartName

NT Authority\LocalService
NT Authority\NetworkService
NT AUTHORITY\NetworkService

Although, if you know you only want a unique list of property names anyway, then use Select-object. Of course you won't get sorted results, but perhaps that doesn't matter:

PS C:\> get-eventlog system -newest 50 | select Source -unique

Service Control Manager

These results are ARE case sensitive:

PS C:\> dir c:\work | select extension -unique | sort extension



You would need to incorporate Sort-Object into your expression.

PS C:\> dir c:\work | select extension -unique | sort Extension

Finally, while technically not a unique related cmdlet, Group-Object by its nature offers uniqueness. This cmdlet puts objects into buckets based on a property and it is not case sensitive:

PS C:\> dir c:\work | where {-Not $_.PSIsContainer} | group extension | sort name | select name


Which cmdlet to use depends on what objects you are processing and what type of data you are trying to retrieve. But as you see, there are many ways to be unique.

About the Author

Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at, and a frequent speaker at technology conferences and user groups.

comments powered by Disqus

SharePoint Watch

Sign up for our newsletter.

Terms and Privacy Policy consent

I agree to this site's Privacy Policy.