Find and Delete Unliked GPOs

Find and Delete Unliked GPOs

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.

 

  1. Import-Module GroupPolicy
  2. $backupPath="C:\Users\jeffrl-p\Desktop\Backup_GPO"

  3. if (-Not(Test-Path -Path $backupPath)) { mkdir $backupPath }

  4. Get-GPO -All | Sort-Object displayname | Where-Object { If ( $_ | Get-GPOReport -ReportType XML | Select-String -NotMatch "<LinksTo>" )     

  5. {
  6.    $backupReportPath = $backupPath + "" + $_.DisplayName + ".html"
  7.    Backup-GPO -Name $_.DisplayName -Path $backupPath
  8.    Get-GPOReport -Name $_.DisplayName -ReportType Html -Path $backupReportPath
  9.    $_.DisplayName | Out-File $backupPath + "UnLinked_GPO_List.txt" -Append
  10.    $_.Displayname | remove-gpo -Confirm
  11.    }
  12. }