Find duplicate user accounts

This topic contains 1 reply, has 2 voices, and was last updated by  Jarkko Vepsäläinen 3 months, 1 week ago.

  • Author
    Posts
  • #74684

    kishor
    Participant

    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

  • #74695

    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

You must be logged in to reply to this topic.