Delete Registry key 'value' if it exists

It seems I have a PS made that will query a reg entry and delete the value of a key if it sees it.
What I don’t know is any remaining script in the PS of what to do if it doesn’t exist.
This PS works fine deleting the value when it’s there, but fails when it’s not.

Get-ItemProperty -Path “HKLM:\SOFTWARE\WOW6432Node\Key” -Name “GUID”
Remove-ItemProperty -Path “HKLM:\SOFTWARE\WOW6432Node\Key” -Name “GUID” -ErrorAction Ignore

I’m even just guessing at assigning Name to the value.

Rick,
Welcome to the forum. :wave:t4:

I’m unsure if I really get what you mean. If the registry value does not exist you cannot delete it.
So you may make sure only to delete it if you find it.

$Path = 'HKLM:\SOFTWARE\WOW6432Node\Key'
$Name = 'GUID'
if (Get-ItemProperty -Path $Path -Name $Name) {
    Remove-ItemProperty -Path $Path -Name $Name
}

BTW: When you post code or sample data or console output please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.
Thanks in advance

Some pc’s will have the value, some won’t. If the script sees the value, it succeeds in deleting it.
However, when the value doesn’t exist, my MDT process breaks because the PS fails, saying it cant find the value. I don’t know how to instruct the PS to move on, exit, whatever, if the value is not there.

You were almost there with the code in your post. If you had added -ErrorAction Ignore to your Get-ItemProperty command, like you have for Remove-ItemProperty, the script would not error.

Suppressing errors like that isn’t ideal though. It would be better to wrap the code in a try/catch block, make the error a terminating error with -ErrorAction Stop, and handle it gracefully.

$Path = 'HKLM:\SOFTWARE\WOW6432Node\Key'
$Name = 'GUID'
try {
    Get-ItemProperty -Path $Path -Name $Name -ErrorAction Stop
    Remove-ItemProperty -Path $Path -Name $Name -ErrorAction Stop
}
catch {
    Write-Warning "$_.Exception.Message"
}

I will give this a try right now. I know a fair amount of PS to get things done I need, but the try and catch are not familiar to me. Will this throw up any messages whether it sees the GUID or not? I’d hope to have this all run silently without notifications or intervention.

Whether you see it depends on how you run the script. Although even if you did see it written to the console, the script would continue. This sort of thing is useful for logging.

If you want to suppress the warning completely, you can set $WarningPreference = 'SilentlyContinue' at the top of your script, alternatively, to suppress for just that warning, use:

Write-Warning "$_.Exception.Message" -WarningAction SilentlyContinue

Edit: remove hyphen from -SilentlyContinue

Ok I will add that. I’m using LiteTouch MDT so everything from beginning to end should just run without messages, unless something actually breaks. But wouldn’t that make the Write-Warning in the first reply redundant? To request a reply but add a “skip” in it to continue?

So what I ended up with was an MDT Summary page that says it was successful but Property GUID does not exist at path. HKLM\SOFTWARE\WOW6432Node\mykey.Exception.Message

I have a PS as such:

Write-Warning “$.Exception.Message" -WarningAction -SilentlyContinue
$Path = ‘HKLM:\SOFTWARE\WOW6432Node\Key’
$Name = ‘GUID’
try {
Get-ItemProperty -Path $Path -Name $Name -ErrorAction Stop
Remove-ItemProperty -Path $Path -Name $Name -ErrorAction Stop
}
catch {
Write-Warning "$
.Exception.Message”
}

I got some "Cannot bind parameter ‘WarningAction’. Cannot convert ‘-SilentlyContinue’ to type System.Management.Automation.ActionPreference.’

Can you try to remove a key’s value in your reg and then run it again with the value gone?

Per @Olaf’s earlier request: please use the </> button and format your code as code. It makes it much easier to read.

I have edited my earlier reply to remove a typo, -SilentlyContinue should not have the leading hyphen.

I see the button now, my apology.
As you’ve said, I should have the Warning Action Silent…at the top and also the
catch {Write-warning “$_Exception…” at the bottom?

No problem, all newcomers struggle with that button. As well as making it easier to read, it makes it easier for us to copy & paste code because you don’t get the curly quote marks.

If you’re setting the preference for the whole script, you put this at the top of the script.

$WarningPreference = 'SilentlyContinue'

If you just want to suppress the specific Write-Warning then the code below goes in the catch block:

Write-Warning "$_.Exception.Message" -WarningAction SilentlyContinue

Ok. Sorry for being so dumb…if I use the Write-Warning at the top and not the one at the bottom,
then I remove the whole {catch section?

You can’t use Write-Warning at the top. If you do that, all it’s going to do is try and write the last error message.

If you don’t want to use the catch block, then you’ll need to delete the try block as well. If you do that, then we’re back to using

Get-ItemProperty -Path $Path -Name $Name -ErrorAction Ignore

That should work fine but it’s not a best practice to ignore errors, hence I gave you an alternative method.

$Path = 'HKLM:\SOFTWARE\WOW6432Node\Fatpot'
$Name = 'GUID'
try {
    Get-ItemProperty -Path $Path -Name $Name -ErrorAction Ignore
    Remove-ItemProperty -Path $Path -Name $Name -ErrorAction Stop
}
catch {
    Write-Warning "$_.Exception.Message"
}
`
Is that what we're looking at? If I remove the catch, then the TRY says it requires it.

I just need it to check for the value “GUID” in the mykey path.
If it exists, remove it. If not, just quit without a message.
The part I am unclear of is what to do in the PS when the GUID doesn’t exist.

$Path = 'HKLM:\SOFTWARE\WOW6432Node\mykey'
$Name = 'GUID'

    Get-ItemProperty -Path $Path -Name $Name -ErrorAction Ignore
    Remove-ItemProperty -Path $Path -Name $Name -ErrorAction Ignore

This works perfectly

You need a subexpression here

Write-Warning “$($_.exception.message)” 

And if you don’t want to see the warning then remove it or do what Matt suggested. You can leave the catch block empty.

1 Like

And since you don’t need it anyway you even can omit the Get-Itemproperty!!! :wink:

1 Like