Friday, 19 June 2015

Get AD Group Members using Powershell

We can get the list of AD Group members using Active Directory Powershell cmdlet Get-ADGroupMember. In this article, I am going to write Powershell script to get list of AD Group members, export group members to CSV file and export AD groups and members to CSV file.

Run the following command to import Active Directory cmdlets.
Import-Module ActiveDirectory
Use the following Powershell command to get the list of specified AD Group members.
Get-ADGroupMember -Identity "Domain Admins" | Select Name, SamAccountName

Export AD Group Members to CSV:

The following Powershell script gets a list of members of specified AD group and exports member details to CSV file. Replace the group name "Domain Admins" with your own group name in the below script.
$GroupName = "Domain Admins"
Get-ADGroupMember -Identity $GroupName | Select Name, SamAccountName |
Export-CSV "C:\\ADGroupMembers.csv" -NoTypeInformation -Encoding UTF8

Export AD Groups and Members to CSV:

The following PowerShell script gets a list of all Active Directory groups and its members. The Get-ADGroup cmdlet gets all the AD Groups and passed into ForEach-Object to get members of every AD Group. Finally it exports both group and member details to CSV file.
$Groups = Get-ADGroup -Filter "*"
$Groups | ForEach-Object {
$group = $_.Name
$members = ''
Get-ADGroupMember $group | ForEach-Object {
        If($members) {
              $members=$members + ";" + $_.Name
           } Else {
New-Object -TypeName PSObject -Property @{
      GroupName = $group
      Members = $members
} | Export-CSV "C:\\AD-Group-Members.csv" -NoTypeInformation -Encoding UTF8

CSV output AD Groups and Members:

Export AD Group Members to CSV using Powershell script


No comments:

Post a Comment