PowerShell: Remove duplicates from array

Remove duplicates in a PowerShell array

Administrators often have to deal with arrays of duplicate values such as when querying for installed software registry keys. Manually identifying the duplicate values and removing them is next to impossible when done at scale. With PowerShell, you can spot and remove the duplicate values from a array easily. Provide the array as the parameter and run the following script to remove the remove duplicates.

Script

<#
.SYNOPSIS
This script can be used to remove duplicates in a powershell array
.DESCRIPTION
This script can be used to remove duplicates in a powershell array
.EXAMPLE
C:\PS> C:\Script\Remove_Duplicates_In_Powershell_Array.ps1 1 1 2 2 3 3 4 4 1 2 3 4
Removes duplicates and displays the output
#>
#use -uniq to remove duplicates from an array.

echo “Array with duplicates”
Write-Host “$args”
$args = $args | select -uniq
echo “Array with duplicates removed”
Write-Host “$args”