Return the unique items from a sorted list.
Syntax
Get-Unique [-inputObject psobject] [-asString] [CommonParameters]
Get-Unique [-inputObject psobject] [-onType] [CommonParameters]
Key
-inputObject
A command/expression/variable that returns the object(s)
(will treat as a single collection)
-asString
Treat the data as a string, not an object.
Use this parameter to find the unique values of object properties
e.g. file names.
For a collection of objects of the same type, Get-Unique will
returns just one (the first).
-onType
Return only one object of each type.
CommonParameters:
-Verbose, -Debug, -ErrorAction, -ErrorVariable, -WarningAction, -WarningVariable,
-OutBuffer -OutVariable.
Standard Aliases for Get-Unique: gu
Examples
Sort an array of integers, and display the unique values:
PS C:\> 2,4,6,8,2,64,4,2,99,3 | sort-object | Get-Unique
List every folder on the C: drive that contains one or more .xls files:
PS C:\> gci C:\ -include *.xls -recurse |foreach {$_.directoryName} | get-unique
Get the names of processes running on the computer with duplicates eliminated:
PS C:\> get-process | sort-object | select processname | get-unique -asstring
Find the number of unique words in a text file:
PS C:\> $a = $(foreach ($line in get-content C:\docs\File1.txt) {$line.tolower().split(" ")}) | sort | get-unique
PS C:\> $a.count
"An Englishman, even if he is alone, forms an orderly queue of one" ~ George Mikes
Related PowerShell Cmdlets:
Get-Random - Get a random number.