Using the Get-Unique Cmdlet

Listing the Unique Members of a Collection

Given a sorted collection of items, the Get-Unique cmdlet returns just the unique items in that collection.

What does that mean? Well, suppose the text file C:\Scripts\Test.txt contains the following:

apple
banana
grapefruit
apple
apple
banana
grapefruit
apple
pear
apple
apple
apple
peach
banana
pear

To determine the unique items in this collection, we can read the text file, sort it, then run the sorted list through Get-Unique. In other words, we can do this:

Get-Content c:\scripts\test.txt | Sort-Object | Get-Unique

And here’s what we get back:

apple
banana
grapefruit
peach
pear
Get-Unique Aliases
  • gu