How to extract Active Directory users with Get-ADGroup and Get-ADGroupMember

01 March,2021 by Tom Collins

Question: I have a large list of Active Directory Groups and would like to extract the users from these Active Directory Groups. How can I script to iterate through a large list of Active Directory Group and list users using Powershell?

Answer: Powershell has a very effective set of Active Directory modules. Within the set of AD modules , it is possible to achieve listing AD group members , group per group , combining  Powershell Get-ADGroup and Get-ADGroupMember

The basic flow is to extract the AD groups - place them  into a DataTable  and then iterate through the DataTable rows , using Get-ADGroupMember .      The best way to illustrate is to use an example.

The first step is to place the Get-ADGroup list into the $dt DataTable.   

The second step is to iterate using the foreach loop and execute the  Get-ADGroupMember per item. 

The end result will be a list of your AD groups and under every AD group the members

For example:

mygroup1

user1

user2

mygroup2

user

 

 

$dt = new-object "System.Data.DataTable"
$dt = Get-ADGroup -Filter "name -like '*.myfilter'" -Properties * | select name

foreach ($Row in $dt) { 

	write-Output "$($Row.Name)"
       $row_name = "$($Row.Name)"
       Get-ADGroupMember -identity “$row_name” | select name 
	
}




 


Author: Tom Collins (http://www.sqlserver-dba.com)


Share:

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment on How to extract Active Directory users with Get-ADGroup and Get-ADGroupMember


sqlserver-dba.com | SQL Server Performance Tuning | SQL Server DBA:Everything | FAQ | Contact|Copyright & Disclaimer