Get-Unique

Return the unique items from a sorted list.

Syntax
      Get-Unique [-inputObject psobject] [-asString] [CommonParameters]
    
      Get-Unique [-inputObject psobject] [-onType] [CommonParameters]

Key
   -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). 

   -inputObject
       The objects to filter. Enter a variable that contains the objects or type
       an expression that returns the objects.

       When the -InputObject parameter is used to submit a collection of items,
       Get-Unique receives one object that represents the collection.
       Because one object cannot be filtered, Get-Unique returns the entire collection unchanged.

       To filter multiple items, pipe them to Get-Unique.

       This parameter is an implementation detail: its purpose is to enable input via the pipeline, and its
       direct use with arrays (collections) does not (yet) provide any useful functionality.

   -onType 
       Return only one object of each type.

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:\> Get-ChildItem C:\ -include *.xls -recurse |
ForEach-Object {$_.directoryName} | Get-Unique

Get the names of processes running on the computer with duplicates eliminated:

PS C:\> Get-Process | sort-object | Select-Object processname | Get-Unique -asstring

Find the number of unique words in a text file:

$a = $(ForEach-Object ($line in Get-Content C:\docs\File1.txt) {
   $line.tolower().split(" ")
}) | Sort-Object | Get-Unique
$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.


 
Copyright © 1999-2024 SS64.com
Some rights reserved