≡ Menu

List duplicate entries count in array using powershell

I have a file with list of users with duplicate entries. My goal is to process this file and generate the duplicate list. I also need to know how many times a entry got repeated in file incase of duplication. This is the requirement I got today and I could able to fulfill this with less efforts using PowerShell.

So, here is the script.

$hash =@{}
$users = Get-Content c:\tempuserslist.txt
$users | % {$hash[$_] = $hash[$_] + 1 }
$hash.getenumerator() | ? { $_.value -gt 1 }

In above example, I used a hash table to keep track of number of entries of each. We can use arrays here but that won’t help you listing the duplicates count. If you would like read more details about hashes, please refer http://technet.microsoft.com/en-us/library/ee692803.aspx

Comments on this entry are closed.

  • eastlandgrl October 18, 2010, 4:08 pm

    interesting, thanks

  • walid2mi February 13, 2011, 1:25 pm


    cat users.txt | select -un

  • chris kuliukas February 28, 2013, 7:06 am

    cat users.txt | group-object