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”

 




    • Related Articles

    • Generate an Activity Report for Microsoft 365 Groups and Teams

      Introduction The activity reports available for Microsoft 365 groups and Teams can be beneficial for administrators in an organization. Microsoft 365 teams group activity reports provide insight into group activities, group workloads, group counts, ...
    • How to find unused Exchange Online mailboxes

      What are unused Exchange Online mailboxes and how to identify them? Unused Exchange Online mailboxes are user mailboxes which are currently not being used by their users. There are 3 ways in which we can identify if a mailbox is unused or not. They ...
    • Remove read only property of a file

      A common issue that administrators face while restoring files from their backups is that the read-only property of the files comes marked by default. This can cause serious trouble as end users will not be able to edit their documents without ...
    • How to add/remove Active Directory users to Active Directory groups using PowerShell

      Active Directory is a powerful tool for managing users and groups in a Windows environment. One of the most often-repeated tasks for administrators is to add or remove users from Active Directory groups. In this article, we will explore how to ...
    • How to create, copy and remove Active Directory users using PowerShell

      One of the most fundamental and often repeated task by an administrator is to create, copy or remove an Active directory user. Thanks to the New-ADUser cmdlet, it is extremely simple. Below are the PowerShell scripts to create, copy and remove an ...