/build/static/layout/Breadcrumb_cap_w.png

Deleting Specific Remote Registry Entry - Powershell

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.


Clear-Host
$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

Answers (2)

Posted by: rileyz 7 years ago
Red Belt
0
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.

http://pastebin.com/SmsiZxKx
Posted by: alphabeta 7 years ago
Black Belt
0
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.

Comments:
  • 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. - rileyz 7 years ago
 
This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ