Home > PowerShell > List duplicate entries count in array using powershell

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

No related content found.

  1. October 18, 2010 at 4:08 pm | #1

    interesting, thanks

  2. walid2mi
    February 13, 2011 at 1:25 pm | #2

    Thanks,

    cat users.txt | select -un

  1. February 17, 2012 at 11:57 pm | #1

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>