Created
May 8, 2020 22:35
-
-
Save jfrantz1-r7/1941622f7e0ec3687e52e336ab37124d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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
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