Powershell detect and delete a registry entry

Thursday 30 June 2022 at 5:25 pm

I'm sure this will be better documented elsewhere on the web, but I like to add snippets here which may help a person with the same query as me.

I wanted to use PS to see if a particular entry in HKLM\Microsoft\Windows\CurrentVersion\Run is there, and if it is, delete it so the program doesn't autorun.  So here's my code:

$path = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\run"
$test = (Get-ItemProperty -Path $path -Name "softwarename") #you can use wildcard * to find
if ($test -ne $null)
{
Write-Host "deleting reg entry"
Remove-ItemProperty $path -Name "softwarename"
}
Else
{
Write-Host "it's not there, bye"
}

It's probably clunky and badly-written, but works for me!