PowerTip: Get Unique Numbers from Array


Summary: Learn how to get unique numbers from an array.

Hey, Scripting Guy! Question How can I use Windows PowerShell to return only the numbers that are unique in an array?

Hey, Scripting Guy! Answer Sort the array of numbers, then pipe the output to the Get-Unique cmdlet, for example:

$a = 1,2,7,6,4,3,3,2,9

$a | sort | Get-Unique

Note  Sort is an alias for the Sort-Object cmdlet.

Comments (1)

  1. Kevin D says:

    Wouldn’t it be easier to just use the -Unique parameter on Sort-Object?

    $a | Sort-Object -Unique

    Or if you don’t want to sort them, use the -Unique parameter on Select-Object

    $a | Select-Object -Unique

Skip to main content