Skip to content

Instantly share code, notes, and snippets.

@AfroThundr3007730
Created April 10, 2020 04:32
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 AfroThundr3007730/677066cb0dbf219997951c8981353ca2 to your computer and use it in GitHub Desktop.
Save AfroThundr3007730/677066cb0dbf219997951c8981353ca2 to your computer and use it in GitHub Desktop.
Nuking orphaned GPOs in the SYSVOL
# Nuking orphaned GPOs in the SYSVOL
Start-Transcript .\gpo-cleanup.log
$guids = (get-gpo -All).id.guid
$files = (ls .).name
Write-Host 'Registered GPOs:' $guids.count
Write-Host 'GPO Files:' ($files | Select-String '{').count
$extraFiles = @()
foreach($i in $files) {
if($i -notmatch '{') {
continue
}
$fileGuid = $i.Substring(1, $i.Length - 2).ToLower()
if(!($guids.ToLower() -contains $fileGuid)) {
Write-Host 'Missing GPO' $fileGuid 'for folder' $i
$extraFiles += $i
}
}
if($extraFiles.count -gt 0) {
Write-Host 'Moving all orphaned GPO files'
mkdir .\orphans
foreach($i in $extraFiles) {
mv $i .\orphans\$i
}
}
Stop-Transcript
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment