public/Optimize-GPOs.ps1
Function Optimize-GPOs { Import-Module grouppolicy Write-Warning "This script will remove all unlinked GPOs, Do you want to Continue (Y/N)?" -WarningAction Inquire Function IsNotLinked($xmldata){ If ($xmldata.GPO.LinksTo -eq $null) { Return $true } Return $false } $unlinkedGPOs = @() Get-GPO -All | ForEach { $gpo = ($_.ID) ; $_ | Get-GPOReport -ReportType xml | ForEach { If(IsNotLinked([xml]$_)){$unlinkedGPOs += $gpo} }} If ($unlinkedGPOs.Count -eq 0) { "No Unlinked GPO's Found. The script has not removed any GPOs" } Else { foreach ($GP in $unlinkedGPOs) { $GpToDelete = Get-GPO -guid $GP Write-Host "Removing $($GpToDelete.DisplayName)" -ForegroundColor Green Remove-GPO -Guid $GP -Verbose } } Write-Host "Finished running the script, $($unlinkedGPOs.Count) unused group policies were removed." } |