Powershell – Export all members of all AD Security Groups to CSV


Powershell is an awesome tool and can help IT professionals in gathering information in a split second whereas otherwise it could take hours manually.

We were recently asked to carry out a number of tasks for information gathering for annual housekeeping. The tasks were varied but were made up of information from Active Directory and Exchange. This meant that we could use Powershell and Exchange Management Shell to get this information quickly and efficiently.

So, over the next few weeks we thought we would share our Powershell and Exchange Management Shell scripts on our blog here so that they can be reused by others.

This is our first script but you can visit our Powershell category to find many more that we have posted on here in the past and the many more we will be posting over the next few weeks.

Export all members of all AD Security Groups to CSV

For our first Powershell script, we were asked to provide a few list of all Active Directory Security Groups, their members and provide it in Excel format. So, below is the script we used to get the information and then export it to CSV file format.

Get-ADGroup -filter "Groupcategory -eq 'Security' -AND GroupScope -ne 'DomainLocal' -AND Member -like '*'" |
foreach {
Write-Host "Exporting $($_.name)" -ForegroundColor Cyan
$name = $_.name -replace " ","-"
$file = Join-Path -path "C:\TGH" -ChildPath "$name.csv"
Get-ADGroupMember -Identity $_.distinguishedname -Recursive |
Get-ADObject -Properties SamAccountname,Title,Department |
Select Name,SamAccountName,Title,Department,DistinguishedName,ObjectClass |
Export-Csv -Path $file -NoTypeInformation


  • “C:\TGH” = CSV output directory
  • $name.csv – this will automated create a filename, or you can amend this how you want it.

What this will do is output a CSV file with all Active Directory Security Groups and all user accounts that are in each security group. You can then if you wish convert this to XLSX within Excel.


If you have any questions or comments on this guide, please feel free to leave us a message below using our comments section.

Recommended...  Customise Shoretel ringtone on a single phone

How useful was this post?

Click on a star to rate it!

Average rating / 5. Vote count:

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

About A.J. Armstrong

Founder of TechyGeeksHome and Head Editor for over eight years! IT expert in multiple areas for over 21 years. Sharing experience and knowledge whenever possible! Making IT Happen.

View all posts by A.J. Armstrong

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.