Scripting Question

Deleting Specific Remote Registry Entry - Powershell

02/07/2017 2456 views
Hi all,
I'm no expert at Powershell but I've been playing about with trying to write a script to delete a specific software GPO deployed to a remote computer. It looks at the GPO name and if it has, for example, Microsoft in the title, it will only delete those specific GPO's. I thought I had it working a few weeks ago but its not working now. It's deleting all deployed GPO's instead of specific ones. Have I removed too many brackets or quotes? I used Write-Host as test checkpoints and it'll write the first $Search string out but not the second within the Scriptblock. Is the -ArgumentList in the right place?

Thanks for your help in advance.

$Search = Read-Host -Prompt 'Software to delete'
Write-Host $Search

        Invoke-Command -ComputerName TestComputer.domain -ScriptBlock {
        Write-Host $Search
        Write-Host '1'
        $Keys = Get-ChildItem 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Group Policy\AppMgmt'
        $Items = $Keys | Foreach-Object {Get-ItemProperty $_.PsPath}
        ForEach ($Item in $Items) {
            Write-Host $Item.'GPO Name'
            Write-Host $Search
            if ($Item.'GPO Name' -match $Search) {
                Remove-Item $Item.PSPath
    } -ArgumentList $Search
0 Comments   [ + ] Show comments


All Answers

Works ok for me, I just tested it then but ran it locally rather than using WinRM.

A few things to note.
  • Dont use write host, it kills kittens. Use Write verbose or debug if you debug info, or if you need output then use Write-Output.
  • Write your debug comments better.

Answered 02/07/2017 by: rileyz
Red Belt

It would be more advantageous to have it working remotely as I can then change the unique name above to loop through our room names which are more generic.
Answered 02/08/2017 by: alphabeta
Black Belt

  • You can change back, I just changed it locally for testing as I didn't want to install WinRM etc. But your script looks fine, remember your doing a *wildcard* match, so if you search, say 'Microsoft', it will remove anything that matches that string. As I always, actually I always say log log log, but in this case - test test test, cover all your test cases.