Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jfrantz1-r7/1941622f7e0ec3687e52e336ab37124d to your computer and use it in GitHub Desktop.
Save jfrantz1-r7/1941622f7e0ec3687e52e336ab37124d to your computer and use it in GitHub Desktop.
$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
@JGillespie-R7
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment