Skip to content

Instantly share code, notes, and snippets.

@PixelRobots
Last active May 24, 2017 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PixelRobots/e603be054e1ff7b60c0e687d4428b0f5 to your computer and use it in GitHub Desktop.
Save PixelRobots/e603be054e1ff7b60c0e687d4428b0f5 to your computer and use it in GitHub Desktop.
.SYNOPSIS
Script used to find all unlinked gpos, back them up and then delete them.
.DESCRIPTION
The script searches your logged on domain for unlinked GPO's. It then backs them up to a folder on the C:\. It will then remove them. It also creates a log file in the same directory.
.LINK
http://www.pixelrobots.com
#>
Import-Module grouppolicy
$domain = get-addomain
$DNS = $domain.DNSRoot
$path= "C:\GPOBackups"
New-Item $Path -type directory -Force
Get-GPO -All -DomainName $DNS | Sort-Object displayname | Where-Object { If ( $_ | Get-GPOReport -ReportType XML | Select-String -NotMatch "<LinksTo>" )
{
$name = $_.DisplayName
$dir = New-Item "$path\$DNS\$name" -Type Directory
Backup-GPO -Guid $_.Id -Path $dir
Get-GPOReport -name $name -ReportType Html -Path "$dir\$name.html"
$_.DisplayName | Out-File "$path\$DNS\UnLinkedGPOS.txt" -Append
$_.Displayname | remove-gpo -Confirm -WhatIf
}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment