powershellactive-directoryout-gridview

PowerShell Out-GridView result


I would like to have a column with the name of the specifics group like ABC_* for each user in the result of Out-GridView.

Get-ADGroup -Filter {Name -like 'ABC_*'} |
    Get-ADGroupMember -Recursive |
    Get-ADUser -Properties mail |
    where { $_.enabled } |
    Out-GridView

In the result, I have all the users of all the ABC_* groups but I don't see which ABC_ groups the users are part of ...


Solution

  • You will need an intermediate loop where you can join the output from Get-ADGroup with the output from Get-ADUser. There is also no need to use Get-ADGroupMember if you use the correct filter. This is one way to do it:

    $filter = -join @(
        '(&' # AND, all conditions must be met
            '(memberOf:1.2.840.113556.1.4.1941:={0})' # all users that are members of this group (recursively)
            '(!userAccountControl:1.2.840.113556.1.4.803:=2)' # and are enabled
        ')' # close AND clause
    )
    
    Get-ADGroup -Filter "Name -like 'ABC_*'" | ForEach-Object {
        $userfilter = $filter -f $_.DistinguishedName
        $parentGroup = $_
    
        Get-ADUser -LDAPFilter $userfilter -Properties mail |
            # add more user attributes here as needed
            Select-Object samAccountName, mail, @{ N='MemberOf'; E={ $parentGroup.samAccountName }}
    } | Out-GridView