PowerShell script block to list all members from a list of groups

This will loop through the groups that it finds that match the “like” section of the script, then output the users from each group with headings to separate. The formatting could be cleaned up a bit on output, but…….meh it works.


$groups = Get-ADGroup -filter {SamAccountName -like "*GROUPNAMEHERE*"}
For ($i=0; $i -lt $groups.Length; $i++){
$groups[$i].SamAccountName
write-host ------------------------
Get-ADGroupMember $groups[$i] | select SamAccountName
write-host ************************
}