PowerShell Logo Small


This is the built-in help made by Microsoft for the command 'Get-Unique', in PowerShell version 5 - as retrieved from Windows version 'Microsoft Windows Server 2012 R2 Standard' PowerShell help files on 2016-06-23.

For PowerShell version 3 and up, where you have Update-Help, this command was run just before creating the web pages from the help files.


Returns unique items from a sorted list.


Get-Unique [-AsString] [-InformationAction {SilentlyContinue | Stop | Continue | Inquire | Ignore | Suspend}] [-InformationVariable [<System.Stringarameter>]] [-InputObject
[<PSObject>]] [<CommonParameters>]
Get-Unique [-InformationAction {SilentlyContinue | Stop | Continue | Inquire | Ignore | Suspend}] [-InformationVariable [<System.Stringarameter>]] [-InputObject
[<PSObject>]] [-OnType] [<CommonParameters>]

Search powershellhelp.space


The Get-Unique cmdlet compares each item in a sorted list to the next item, eliminates duplicates, and returns only one instance of each item. The list must be sorted for
the cmdlet to work properly.

Get-Unique is case-sensitive. As a result, strings that differ only in character casing are considered to be unique.



Online Version: http://go.microsoft.com/fwlink/p/?linkid=293978




-------------------------- EXAMPLE 1 --------------------------

PS C:\>$a = $(foreach ($line in get-content C:\Test1\File1.txt) {$line.tolower().split(" ")}) | sort | get-unique
PS C:\>$a.count

These commands find the number of unique words in a text file.

The first command gets the content of the File.txt file. It converts each line of text to lowercase letters and then splits each word onto a separate line at the space ("
"). Then, it sorts the resulting list alphabetically (the default) and uses the Get-Unique cmdlet to eliminate any duplicate words. The results are stored in the $a variable.

The second command uses the Count property of the collection of strings in $a to determine how many items are in $a.

-------------------------- EXAMPLE 2 --------------------------

PS C:\>1,1,1,1,12,23,4,5,4643,5,3,3,3,3,3,3,3 | sort-object | Get-Unique

This command finds the unique members of the set of integers. The first command takes an array of integers typed at the command line, pipes them to the Sort-Object cmdlet to
be sorted, and then pipes them to Get-Unique, which eliminates duplicate entries.

-------------------------- EXAMPLE 3 --------------------------

PS C:\>get-childitem | sort-object {$_.GetType()} | unique -OnType

This command uses the Get-ChildItem cmdlet to retrieve the contents of the local directory, which includes files and directories. The pipeline operator (|) sends the results
to the Sort-Object cmdlet. The "$_.GetType()" statement applies the GetType method to each file or directory. Then, Sort-Object sorts the items by type. Another pipeline
operator sends the results to Get-Unique. The OnType parameter directs Get-Unique to return only one object of each type.

-------------------------- EXAMPLE 4 --------------------------

PS C:\>get-process | sort-object | select processname | get-unique -asstring

This command gets the names of processes running on the computer with duplicates eliminated.

The Get-Process command gets all of the processes on the computer. The pipeline operator (|) passes the result to Sort-Object, which, by default, sorts the processes
alphabetically by ProcessName. The results are piped to the Select-Object cmdlet, which selects only the values of the ProcessName property of each object. The results are
then piped to Get-Unique to eliminate duplicates.

The AsString parameter tells Get-Unique to treat the ProcessName values as strings. Without this parameter, Get-Unique treats the ProcessName values as objects and returns
only one instance of the object, that is, the first process name in the list.