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

powershell

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.

Free Subscription!Subscribe to our site to receive updates via email!

Enter your email address below and click the Subscribe button to receive email notifications about new and exciting downloads available through our website at https://www.techygeekshome.com

You will also receive notifications about new technical guides, latest news and MSI installer downloads that are available through our blog here at https://blog.techygeekshome.info 

We will not spam you and we will not pass on any of your details to anyone else. We tend to post new content once or twice a week.

Join 142 other subscribers

You can unsubscribe any time you like.

About A.J. Armstrong

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

View all posts by A.J. Armstrong

Leave us a message...

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