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

powershell

Last updated on April 1st, 2023 at 08:40 pm

Read Time:1 Minute, 42 Second

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
}

where:

  • “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.

Feedback

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

Click to rate this post!
[Total: 1 Average: 5]

Free Subscription

If you want to be notified when we post more quality guides like this one, sign up to our free subscription service and you will receive an email when a new post is live.

Join 441 other subscribers.

No need to worry, we will not be filling your inbox with spam and you can unsubscribe anytime you like.


Leave us a message...

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