ADGroup Membership test working partially.

Welcome Forums General PowerShell Q&A ADGroup Membership test working partially.

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

2 years, 11 months ago.

  • Author
  • #35184

    Points: 1
    Rank: Member

    Attempting to AD User's membership in specified group (true/False). Fails when attempting to run via pipeline. Works when split up in parts. Need another pair of eyes to review Failing code below.

    # Working Code
    $GroupName = (Get-ADGroup "Domain Admins").DistinguishedName
    $User = get-aduser "Username" -Properties memberof
    $user.memberof -contains $GroupName

    # Failing code
    get-aduser "Username" -Properties memberof | select name, @{n="IsMember"; E="($_.memberof -contains {(Get-ADGroup 'Domain Admins').DistinguishedName})"}

  • #35185

    Points: 0
    Rank: Member

    You should use {} not ""

    get-aduser "username" -Properties memberof | select name, @{n="IsMember"; E={$_.memberof -contains (Get-ADGroup "Domain Admins").DistinguishedName }}

  • #35187

    Points: 30
    Rank: Member

    Another way you could do it:

    (Get-ADUser -Identity 'Username' -Properties memberOf).memberOf -contains (Get-ADGroup -Identity 'Domain Admins').DistinguishedName

    This will just output true/false.

  • #35245

    Points: 1
    Rank: Member

    Thank you for your quick responses. Tested both ways, both worked great!

The topic ‘ADGroup Membership test working partially.’ is closed to new replies.