Get-Unique
Returns unique items from a sorted list.
Syntax
Get-Unique
[-InputObject <PSObject>]
[-AsString]
[-CaseInsensitive]
[<CommonParameters>]
Get-Unique
[-InputObject <PSObject>]
[-OnType]
[-CaseInsensitive]
[<CommonParameters>]
Description
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.
By default, Get-Unique
is case-sensitive. As a result, strings that differ only in character
casing are considered to be unique.
Examples
Example 1: Get unique words in a text file
These commands find the number of unique words in a text file.
$A = $( foreach ($line in Get-Content C:\Test1\File1.txt) {
$line.tolower().split(" ")
}) | Sort-Object | Get-Unique
$A.count
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: Get unique integers in an array
This command finds the unique members of the set of integers.
1,1,1,1,12,23,4,5,4643,5,3,3,3,3,3,3,3 | Sort-Object | Get-Unique
1
3
4
5
12
23
4643
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: Get unique object types in a directory
This command uses the Get-ChildItem
cmdlet to retrieve the contents of the local directory, which
includes files and directories.
Get-ChildItem | Sort-Object {$_.GetType()} | Get-Unique -OnType
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: Get unique processes
This command gets the names of processes running on the computer with duplicates eliminated.
Get-Process | Sort-Object | Select-Object processname | Get-Unique -AsString
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.
Example 5: Use case-sensitive comparisons to get unique strings
This example uses case-insensitive comparisons to get unique strings from an array of strings.
"aa", "Aa", "Bb", "bb", "aa" | Sort-Object -CaseSensitive | Get-Unique
aa
Aa
bb
Bb
Example 6: Use case-insensitive comparisons to get unique strings
This example uses case-insensitive comparisons to get unique strings from an array of strings.
"aa", "Aa", "Bb", "bb", "aa" | Sort-Object | Get-Unique -CaseInsensitive
aa
Bb
Parameters
-AsString
Indicates that this cmdlet uses the data as a string. Without this parameter, data is treated as an
object, so when you submit a collection of objects of the same type to Get-Unique
, such as a
collection of files, it returns just one (the first). You can use this parameter to find the unique
values of object properties, such as the file names.
Type: | SwitchParameter |
Position: | Named |
Default value: | False |
Required: | False |
Accept pipeline input: | False |
Accept wildcard characters: | False |
-CaseInsensitive
By default, Get-Unique
is case-sensitive. When you use this parameter, the cmdlet uses
case-insensitive comparisons.
This parameter was added in PowerShell 7.4.
Type: | SwitchParameter |
Position: | Named |
Default value: | False |
Required: | False |
Accept pipeline input: | False |
Accept wildcard characters: | False |
-InputObject
Specifies input for Get-Unique
. Enter a variable that contains the objects or type a command or
expression that gets the objects.
This cmdlet treats the input submitted using InputObject as a collection. It doesn't enumerate individual items in the collection. Because the collection is a single item, input submitted using InputObject is always returned unchanged.
Type: | PSObject |
Position: | Named |
Default value: | None |
Required: | False |
Accept pipeline input: | True |
Accept wildcard characters: | False |
-OnType
Indicates that this cmdlet returns only one object of each type.
Type: | SwitchParameter |
Position: | Named |
Default value: | None |
Required: | False |
Accept pipeline input: | False |
Accept wildcard characters: | False |
Inputs
You can pipe any type of object to this cmdlet.
Outputs
This cmdlet returns its input objects without duplicates.
Notes
PowerShell includes the following aliases for Get-Unique
:
- All platforms:
gu
For more information, see about_Aliases.
To sort a list, use Sort-Object
. You can also use the Unique parameter of Sort-Object
to
find the unique items in a list.
Related Links
Σχόλια
https://aka.ms/ContentUserFeedback.
Σύντομα διαθέσιμα: Καθ' όλη τη διάρκεια του 2024 θα καταργήσουμε σταδιακά τα ζητήματα GitHub ως μηχανισμό ανάδρασης για το περιεχόμενο και θα το αντικαταστήσουμε με ένα νέο σύστημα ανάδρασης. Για περισσότερες πληροφορίες, ανατρέξτε στο θέμα:Υποβολή και προβολή σχολίων για