Cleaning up Unlinked GPOs using PowerShell
Unlinked GPO's, otherwise called orphaned GPOs are not linked to any Active Directory sites, domains, or organizational units (OUs). To minimize management overhead, these unlinked GPO's should be deleted as they take up considerable space in the Domain Controllers due to replication. Besides this, a cluttered AD environment is difficult to maintain and leads to administrative confusion.
Fortunately, administrators can use PowerShell scripts to spot and delete unlinked GPO's easily. The following script will do the trick.
- Import-Module GroupPolicy
- $backupPath="C:\Users\jeffrl-p\Desktop\Backup_GPO"
- if (-Not(Test-Path -Path $backupPath)) { mkdir $backupPath }
- Get-GPO -All | Sort-Object displayname | Where-Object { If ( $_ | Get-GPOReport -ReportType XML | Select-String -NotMatch "<LinksTo>" )
- {
- $backupReportPath = $backupPath + "" + $_.DisplayName + ".html"
- Backup-GPO -Name $_.DisplayName -Path $backupPath
- Get-GPOReport -Name $_.DisplayName -ReportType Html -Path $backupReportPath
- $_.DisplayName | Out-File $backupPath + "UnLinked_GPO_List.txt" -Append
- $_.Displayname | remove-gpo -Confirm
- }
- }