Skip to content

Instantly share code, notes, and snippets.

@giridharvedula
Forked from 9to5IT/Manage-ADGroups.ps1
Created October 17, 2023 16:27
Show Gist options
  • Select an option

  • Save giridharvedula/54cc226520f8e2c6e92572ec2ade639f to your computer and use it in GitHub Desktop.

Select an option

Save giridharvedula/54cc226520f8e2c6e92572ec2ade639f 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