AD Script to get all members of groups with same naming convention

EliseF 0 Reputation points
2023-08-24T19:09:36.5733333+00:00

Hello,

I am trying to run a script to tell me all the members of groups that start with a certain naming convention.
I basically want to combine Get-ADGroupMember with Get-ADGroup -Filter {name -like "*_"}

So i want a query to pull all members of groups that start with lets say "ABC" --would want the group name and all members exported.

Any help would be greatly appreciated.

Active Directory
Active Directory
A set of directory-based technologies included in Windows Server.
5,838 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,041 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Rich Matheisen 44,776 Reputation points
    2023-08-24T21:38:37.34+00:00

    Something like this?

    Get-ADGroup -filter "name -like '*_*'" |    # matches _ABC, ABC_, ABC_DEF . . . I.E., anything with a _ character in the name
        ForEach-Object{
            $n = $_.name
            $m = @()
            Get-ADGroupMember $_.DistinguishedName |
                ForEach-Object{
                    $m += $_.SamAccountName
                }
            [PSCustomObject]@{
                GroupName = $n
                Members = $m -join ";"
            }
        } | Export-Csv c:\junk\mem.csv -NoTypeInformation
    
    
    0 comments No comments