Export AD Group Members to CSV using Powershell

We can export AD groups and its members using the Active Directory Powershell cmdlets Get-ADGroup and Get-ADGroupMember.

Use the following command to enable Active Directory cmdlets.

Import-Module ActiveDirectory

Get specified AD Group members:

Use the below Powershell command to get the list of members of specified AD Group.

Get-ADGroupMember -Identity "Domain Admins" | Select Name, SamAccountName

Export AD Groups and Members to CSV

The following PowerShell script gets a list of all AD groups and its members. The Get-ADGroup cmdlet gets all the AD Groups and the list of group values passed into ForEach-Object to get members of every AD Group. Finally it exports both groups and members as semi-colon(;) separated values to CSV file using Export-CSV cmdlet.

$Groups = Get-ADGroup -Filter "*"
$Groups | ForEach-Object {
$group = $_.Name
$members = ''
Get-ADGroupMember $group | ForEach-Object {
        If($members) {
              $members=$members + ";" + $_.Name
           } Else {
              $members=$_.Name
           }
  }
New-Object -TypeName PSObject -Property @{
      GroupName = $group
      Members = $members
     }
} | Export-CSV "C:\AD-Group-Members.csv" -NoTypeInformation -Encoding UTF8

Sample CSV output of Export AD Groups and Members:

Export AD Group Members to CSV using Powershell script
Advertisement

Leave a Comment