Created
April 10, 2020 04:32
-
-
Save AfroThundr3007730/677066cb0dbf219997951c8981353ca2 to your computer and use it in GitHub Desktop.
Nuking orphaned GPOs in the SYSVOL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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