-
-
Save giridharvedula/54cc226520f8e2c6e92572ec2ade639f to your computer and use it in GitHub Desktop.
PowerShell: Cleanup empty AD Groups
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
| 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