PowerShell: Identify unused GPO with ZZZ_ - REVISITED

Sometime ago now I posted this which checks all your GPO's and renames any that are un-linked with the prefix ZZZ_, very handy.

It has one major flaw that needs addressing. I am using the GPO name when I should be using the GUID.

It doesn't sound like this would be much of a problem but I recently did some work somewhere where they had multiple GPO's with the same name and this script failed me.

So here it is with the GUID and as a bonus, rather than renaming orphans creates a report so you can submit the results to your CAB before making any rash changes to anything .

$Orphans = Get-GPO -All | Sort-Object guid | Where-Object { If ( $_ | Get-GPOReport -ReportType XML | Select-String -NotMatch "<LinksTo>" ) {$_.DisplayName } }

Foreach ($Orphan in $Orphans)
$Newname = $null
$Oldname = $Orphan.DisplayName
$Newname = "ZZZ_" + $Oldname
echo $Newname
Rename-GPO -Guid $Orphan.id -TargetName $Newname

As a double bonus I have also corrected the spelling of orphans in the code.

How much do I spoil you.