Powered by Blogger.

Tuesday, September 3, 2019

Export all AD groups and thier members to CSV file

Sometime it;s hard to manage user Group and their locations so today we run some powershell script and get all Active directory Group in CSV

Export all Active Directory groups, the group catagory, group scope and all group members to a CSV file. Active Directory PowerShell module is required. Tested on Windows Server 2012 R2 and Windows Server 2016. Amend $DateTime variable format and $CSVFile for

#// Start of script
#// Get year and month for csv export file
$DateTime = Get-Date -f "yyyy-MM"

#// Set CSV file name
$CSVFile = "C:\AD_Groups"+$DateTime+".csv"

#// Create emy array for CSV data
$CSVOutput = @()

#// Get all AD groups in the domain
$ADGroups = Get-ADGroup -Filter *

#// Set progress bar variables
$tot = $ADGroups.count

foreach ($ADGroup in $ADGroups) {
#// Set up progress bar
$status = "{0:N0}" -f ($i / $tot * 100)
Write-Progress -Activity "Exporting AD Groups" -status "Processing Group $i of $tot : $status% Completed" -PercentComplete ($i / $tot * 100)

#// Ensure Members variable is empty
$Members = ""

#// Get group members which are also groups and add to string
$MembersArr = Get-ADGroup -filter {Name -eq $ADGroup.Name} | Get-ADGroupMember | select Name
if ($MembersArr) {
foreach ($Member in $MembersArr) {
$Members = $Members + "," + $Member.Name
$Members = $Members.Substring(1,($Members.Length) -1)

#// Set up hash table and add values
$HashTab = $NULL
$HashTab = [ordered]@{
"Name" = $ADGroup.Name
"Category" = $ADGroup.GroupCategory
"Scope" = $ADGroup.GroupScope
"Members" = $Members

#// Add hash table to CSV data array
$CSVOutput += New-Object PSObject -Property $HashTab

#// Export to CSV files
$CSVOutput | Sort-Object Name | Export-Csv $CSVFile -NoTypeInformation

#// End of script

Happy Script day :) 

No comments:

Post a Comment