Skip to content

Instantly share code, notes, and snippets.

@9to5IT
Created July 4, 2016 12:08
Show Gist options
  • Save 9to5IT/be02956ca8f388e9150a5af097304b74 to your computer and use it in GitHub Desktop.
Save 9to5IT/be02956ca8f388e9150a5af097304b74 to your computer and use it in GitHub Desktop.
PowerShell: Cleanup empty AD Groups
Import-Module ActiveDirectory
#-------------------------------
# FIND EMPTY GROUPS
#-------------------------------
# Get empty AD Groups within a specific OU
$Groups = Get-ADGroup -Filter { Members -notlike "*" } -SearchBase "OU=GROUPS,DC=testlab,DC=com" | Select-Object Name, GroupCategory, DistinguishedName
#-------------------------------
# REPORTING
#-------------------------------
# Export results to CSV
$Groups | Export-Csv C:\Temp\InactiveGroups.csv -NoTypeInformation
#-------------------------------
# INACTIVE GROUP MANAGEMENT
#-------------------------------
# Delete Inactive Groups
ForEach ($Item in $Groups){
Remove-ADGroup -Identity $Item.DistinguishedName -Confirm:$false
Write-Output "$($Item.Name) - Deleted"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment