Find duplicate user accounts

Hi,
I have duplicate user accounts need to delete the one whose creation date is older. I have their samaccountnames, how do I get their creation date, compare them and delete the older one, please help.

Thanks,
Kishor

Hi

CompareTo could lead you to somewhere possible? I’m sure there’s better ways to solve this, but using these might help you.

$a = Get-ADUser user1 -Properties whencreated | select samaccountName, whenCreated
$a.whencreated
monday 20. march 2017 16.28.28

$b = Get-ADUser user2 -Properties whencreated | select samaccountName, whenCreated
$b.whencreated
monday 20. march 2017 16.28.27

($a.whenCreated).CompareTo($b.whenCreated)
1
($b.whenCreated).CompareTo($a.whenCreated)
-1

If (($a.whenCreated).CompareTo($b.whenCreated) -lt 0) {"$($a.samaccountname) is older"}
Else {"$($b.samaccountname) is older"}

I wouldn’t go to production with this atleast without proper test, but seems work no my lab.

Building if or switch around these with where could help you. I’m curious how could you have duplicate samaccountnames?

Regards

Jarkko