Share via


Get AD members of each AD group in list

Question

Friday, October 27, 2017 1:27 PM

I have a list of groups I'd like to retrieve members from in a readable format, this script get me close but I'm unable to modify to pipe in a list (txt file) of groups.  Hoping someone knows the quick fix.

Import-Module ActiveDirectory

$Groups = (Get-AdGroup -filter * | Where {$_.name -like "**"} | select name -expandproperty name)


$Table = @()

$Record = [ordered]@{
"Group Name" = ""
"Name" = ""
"Username" = ""
}



Foreach ($Group in $Groups)
{

$Arrayofmembers = Get-ADGroupMember -identity $Group | select name,samaccountname

foreach ($Member in $Arrayofmembers)
{
$Record."Group Name" = $Group
$Record."Name" = $Member.name
$Record."UserName" = $Member.samaccountname
$objRecord = New-Object PSObject -property $Record
$Table += $objrecord

}

}

$Table | export-csv "C:\temp\SecurityGroups.csv" -NoTypeInformation

All replies (7)

Monday, October 30, 2017 1:28 PM ✅Answered | 1 vote

You do not have to seek SamAccountName.  It codes for free.

Get-content C:\aaa.txt |
    ForEach-Object{
        $groupname = $_
        Get-ADGroupMember $groupname -Recursive |
        Where-Object{ $_.objectClass -eq 'User' } |
        Select-Object *, @{ n = 'GroupName'; e = { $groupname } }
    } |
    Select-Object GroupName, samAccountName | 
    Export-csv "C:\output.csv" -NoTypeInformation

\(ツ)_/


Friday, October 27, 2017 1:34 PM | 1 vote

This exact question comes up once a week here.  If you look in the Gallery you will find many scripts that do this.

Here is one solution from 3 days ago.

Get-AdGroup -filter * | 
    ForEach-Object{
        $groupname = $_.Name
        Get-ADGroupMember $_ |
            Where{$_.objectClass -eq 'User'} |
            Get-ADUser -Properties Title, Department |
            Select *,@{n='GroupName';e={$groupname}}
    } |
    Format-Table GroupName, Name, samAccountName, Title, Department

\(ツ)_/


Friday, October 27, 2017 2:07 PM

Thanks - though looking for the specifics on the method of import/export more than anything else, I could have been more clear.  Using the script you have above I'm attempting the export this way, though end up with a blank output file:

Get-content C:\aaa.txt |

ForEach-Object{
        $groupname = $_
        Get-ADGroupMember $groupname -Recursive |
            Where{$_.objectClass -eq 'User'} |
            Get-ADUser -Properties Title, Department |
            Select *,@{n='GroupName';e={$groupname}}
    } |
    Format-Table GroupName, Name, samAccountName

$outfile | select-object GroupName, Name, samAccountName | export-csv -path C:\output.csv -NoTypeInformation

Friday, October 27, 2017 7:03 PM

This got me what I needed, for reference:

Get-content C:\aaa.txt |

ForEach-Object{
        $groupname = $_
        Get-ADGroupMember $groupname -Recursive |
            Where{$_.objectClass -eq 'User'} |
            Get-ADUser -Properties samaccountname |
            Select *,@{n='GroupName';e={$groupname}}
        
    } |

    
Select GroupName, samAccountName | Export-csv "C:\output.csv" -NoType

Friday, October 27, 2017 7:53 PM

Why do you want to export it then re-import it?  Totally in necessary.

\(ツ)_/


Monday, October 30, 2017 1:03 PM

Not sure what you mean, where do you see a re-import?


Monday, October 30, 2017 1:26 PM

Sorry - ignore that. 

Forget all of the odd spaces in your code so it reads correctly.

Get-content C:\aaa.txt |
    ForEach-Object{
        $groupname = $_
        Get-ADGroupMember $groupname -Recursive |
        Where-Object{ $_.objectClass -eq 'User' } |
        Get-ADUser -Properties samaccountname |
        Select-Object *, @{ n = 'GroupName'; e = { $groupname } }
    } |
        Select-Object GroupName, samAccountName |
        Export-csv "C:\output.csv" -NoTypeInformation

This visually models the code structure correctly.

\(ツ)_/