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)
Please log in to answer
Posted by:
rileyz
7 years ago
Posted by:
alphabeta
7 years ago
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