Author Posts

February 11, 2016 at 3:09 pm

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})"}

February 11, 2016 at 3:21 pm

You should use {} not ""

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

February 11, 2016 at 3:57 pm

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.

February 14, 2016 at 7:35 am

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