Get-AdUser based on a variable with a list of names

Welcome Forums General PowerShell Q&A Get-AdUser based on a variable with a list of names

This topic contains 4 replies, has 4 voices, and was last updated by

2 years, 11 months ago.

  • Author
  • #45652

    Topics: 10
    Replies: 20
    Points: 0
    Rank: Member

    I have a variable that contains a list of AD users i.e "Jay Jones". I want to query AD and check that each user in this list has a corresponding account. The problem is that Get-ADUser looks for the users Identity (SamAccountName), and this is done in the format "jayj". So when i issue a command it comes back that none of the users have accounts:

    $UserList | Get-AdUser
    Get-ADUser : Cannot find an object with identity: 'Jay Jones' under: 'DC=domain,DC=com'.
    At line:1 char:12
    + $UserList | Get-ADUser
    +            ~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (Jay Jones:ADUser) [Get-ADUser], ADIdentityNotFoundException
        + FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException,Microsoft.ActiveDirectory.Management.Commands.GetADUser

    I know that the format "Jay Jones" matches the property type 'Name' of my AD User account. How can i make Get-ADUser reference the $UserList variable based on the 'Name' property of the account rather than the Identity (SamAccountName)? I cant change the format of the users in the variable to the same as the SamAccountName, otherwise i would!

  • #45657

    Topics: 3
    Replies: 264
    Points: 78
    Helping Hand
    Rank: Member
    foreach ($user in $userlist){Get-ADUser -Filter {Name -eq $user}}
  • #45675

    Topics: 13
    Replies: 4872
    Points: 1,812
    Helping HandTeam Member
    Rank: Community Hero

    To elaborate:

    If $userlist is a list of string objects, you need to look and see what Get-ADUser will do with those in terms of pipeline binding ByValue. Looking at the help, it's -Identity that's accepting strings ByValue, not -Name. That's why you're getting the error you are. As the answer above demonstrates, you have to manually push the input to -Name by enumerating the list.

    If $userlist had contained values acceptable to -Identity, your first try would have worked.

  • #46186

    Topics: 10
    Replies: 20
    Points: 0
    Rank: Member

    Thanks for the explanation.

  • #46198

    Topics: 18
    Replies: 573
    Points: 32
    Rank: Member

    get-aduser -filter "anr -eq 'Jay Jones'"

The topic ‘Get-AdUser based on a variable with a list of names’ is closed to new replies.