$DateTime = Get-Date -f "yyyy-MM" | |
$CSVFile = "C:\AD_Groups"+$DateTime+".csv" | |
$CSVOutput = @() | |
$ADGroups = Get-ADGroup -Filter * | |
$i=0 | |
$tot = $ADGroups.count | |
foreach ($ADGroup in $ADGroups) { | |
$i++ | |
$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) | |
$Members = "" | |
$MembersArr = Get-ADGroup -filter {Name -eq $ADGroup.Name} | Get-ADGroupMember | select Name, objectClass, distinguishedName | |
if ($MembersArr) { | |
foreach ($Member in $MembersArr) { | |
if ($Member.objectClass -eq "user") { | |
$MemDN = $Member.distinguishedName | |
$UserObj = Get-ADUser -filter {DistinguishedName -eq $MemDN} | |
if ($UserObj.Enabled -eq $False) { | |
continue | |
} | |
} | |
$Members = $Members + "," + $Member.Name | |
} | |
if ($Members) { | |
$Members = $Members.Substring(1,($Members.Length) -1) | |
} | |
} | |
$HashTab = $NULL | |
$HashTab = [ordered]@{ | |
"Name" = $ADGroup.Name | |
"Category" = $ADGroup.GroupCategory | |
"Scope" = $ADGroup.GroupScope | |
"Members" = $Members | |
} | |
$CSVOutput += New-Object PSObject -Property $HashTab | |
} | |
$CSVOutput | Sort-Object Name | Export-Csv $CSVFile -NoTypeInformation |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
I changed line 5 to limit the amount of groups to just those in a specific OU using -SearchBase "OU=Distribution Groups,DC=tor,DC=rapid7,DC=com"
Then added a where cmdlet to line 16 to feed the output of Get-ADGroupMember into.
"| where {$_.objectclass -eq "group"}"
Worked a charm