Prof. Powershell

Uniqueness Counts

PowerShell's Select-Object command has a -unique switch that lest you sort through the riffraff.

You probably spend a lot of time with PowerShell working with collections of objects such as files, service and processes. Depending on your command, you might even end up with duplicates. Try this in your PowerShell session:

PS C:\> get-wmiobject win32_service | Select Name,StartName

You should get a list of services and the account used for each service. I'm expecting you will see duplicates. What if you want a list of names but without the duplicates, that is, a unique names list? There are few ways you can accomplish this.

First, you can use the Select-Object cmdlet, which has a -unique parameter that will return a unique list based on a property you specify:

PS C:\> get-wmiobject win32_service | Select -unique startname | sort

startname
---------
BIGCOMPANY\administrator
NT Authority\NetworkService
NT AUTHORITY\NetworkService
NT AUTHORITY\LocalService
LocalSystem

I piped the result of Select-Object to Sort-Object to produce a sorted list. Now I can see at a glance what accounts are in use. Let's look at another example:

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

Source
------
BROWSER
DCOM
Dhcp
DnsApi
EventLog
LSASRV
Service Control Manager
USER32
W32Time
WinDefend

In this expression, I'm invoking Get-Eventlog to return the last 100 records from the System log. To tidy up the output, I'll sort on the Source property and then pipe the results to Select-Object looking for unique Source property values. The resulting list displays the sources of my problems, at least for the last 100 event log entries.

Here's one final example:

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

Extension
---------
.bmp
.csv
.exe
.html
.msi
.ps1
.txt
.xml

The output is a list of all file types in my Test directory. You can get equivalent results using the Group-Object cmdlet with the added bonus of a count for each file type:

PS C:\> dir c:\test | sort extension | group extension

Count Name     Group
----- ----     -----
    1 .bmp     {jfrost.bmp}
    2 .csv     {services.csv, alias.csv}
    3 .exe     {ADSIBrowser.exe, jdhpsh.exe, getlocalusers.exe}
   10 .html    {jhicks-xpdesk01.html, rgbivgpo.html, testrsop.html,
    1 .msi     {WindowsServer2003-KB340178-SP2-x86-ENU.msi}
    1 .ps1     {testperm.ps1}
    1 .txt     {t.txt}
    1 .xml     {history.xml}


Which approach you take depends on what you intend to do with the information. Next time I'll show you another way to obtain uniqueness.

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 Petri.com, and a frequent speaker at technology conferences and user groups.

comments powered by Disqus
Most   Popular