Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell script to return members of multiple security groups

I need to return all members of multiple security groups using PowerShell. Handily, all of the groups start with the same letters.

I can return a list of all the relevant security groups using the following code:

Get-ADGroup -filter 'Name -like"ABC*"' | Select-Object Name

And I know I can return the membership list of a specific security group using the following code:

Get-ADGroupMember "Security Group Name" -recursive | Select-Object Name

However, I can't seem to put them together, although I think what I'm after should look something like this (please feel free to correct me, that's why I'm here!):

$Groups = Get-ADGroup -filter 'Name -like"ABC*"' | Select-Object Name
ForEach ($Group in $Groups) {Get-ADGroupMember -$Group -recursive | Select-Object Name

Any ideas on how to properly structure that would be appreciated!

Thanks,

Chris

like image 282
Chris Avatar asked Jun 19 '13 15:06

Chris


People also ask

How do I get members of the security group list?

If you want to confirm, go to ADUC and open the security group SG_Office. Click Members. The exact list of users and security groups will show.

How do I get a list of members of AD group in PowerShell?

Use Get-ADGroupMember cmdlet to List Members of an Active Directory Group. The PowerShell Get-ADGroupMember cmdlet is used to list the members of an Active Directory group. You can just type the cmdlet in a PowerShell window and you'll be prompted to enter the name of the group you want to use.

How do I Export Active Directory Security Group members?

Export AD group members to CSVRun PowerShell as administrator. Change the path to the scripts folder. Run the PowerShell script to export AD group members to CSV file. Wait till it completes.

How do I get security group details in PowerShell?

Use the Get-Group cmdlet to view existing group objects in your organization. This cmdlet returns security groups, mail-enabled security groups, distribution groups, and role groups. For information about the parameter sets in the Syntax section below, see Exchange cmdlet syntax.


2 Answers

If you don't care what groups the users were in, and just want a big ol' list of users - this does the job:

$Groups = Get-ADGroup -Filter {Name -like "AB*"}

$rtn = @(); ForEach ($Group in $Groups) {
    $rtn += (Get-ADGroupMember -Identity "$($Group.Name)" -Recursive)
}

Then the results:

$rtn | ft -autosize
like image 174
AndyMeFul Avatar answered Dec 21 '22 15:12

AndyMeFul


This is cleaner and will put in a csv.

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
like image 27
Joseph Alves Avatar answered Dec 21 '22 14:12

Joseph Alves