Cleaning up unlinked Group Policy objects in Active Directory with Powershell

Just a quick script below to run through all unlinked GPO’s in a domain, back them up, export a report and then remove them.

Sweet and short but useful. (# Comment the remove line if you don’t want to remove any)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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

   }

}

Leave a Reply

Your email address will not be published. Required fields are marked *