Created
July 4, 2016 12:08
-
-
Save 9to5IT/be02956ca8f388e9150a5af097304b74 to your computer and use it in GitHub Desktop.
PowerShell: Cleanup empty AD Groups
This file contains 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
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