# Delete Registry key 'value' if it exists

**URL:** <https://forums.powershell.org/t/delete-registry-key-value-if-it-exists/18158>\
**Category:** PowerShell Help\
**Created:** [December 15, 2021, 2:04pm UTC](https://forums.powershell.org/t/delete-registry-key-value-if-it-exists/18158 "2021-12-15T14:04:51Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![mdtuser](https://avatars.discourse-cdn.com/v4/letter/m/3bc359/32.png) [@mdtuser](https://forums.powershell.org/u/mdtuser)\
**Post date:** [December 15, 2021, 2:04pm UTC](https://forums.powershell.org/t/delete-registry-key-value-if-it-exists/18158/1 "2021-12-15T14:04:51Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [December 15, 2021, 2:25pm UTC](https://forums.powershell.org/t/delete-registry-key-value-if-it-exists/18158/2 "2021-12-15T14:25:46Z")

</div>

Rick,  
Welcome to the forum. 👋🏽

> [@mdtuser](#):
>
> 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.

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.

```auto
$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

---

<div class="post-metadata">

**Author:** ![mdtuser](https://avatars.discourse-cdn.com/v4/letter/m/3bc359/32.png) [@mdtuser](https://forums.powershell.org/u/mdtuser)\
**Post date:** [December 15, 2021, 3:24pm UTC](https://forums.powershell.org/t/delete-registry-key-value-if-it-exists/18158/3 "2021-12-15T15:24:08Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [December 15, 2021, 4:29pm UTC](https://forums.powershell.org/t/delete-registry-key-value-if-it-exists/18158/4 "2021-12-15T16:29:42Z")

</div>

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.

```auto
$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"
}

```

---

<div class="post-metadata">

**Author:** ![mdtuser](https://avatars.discourse-cdn.com/v4/letter/m/3bc359/32.png) [@mdtuser](https://forums.powershell.org/u/mdtuser)\
**Post date:** [December 15, 2021, 4:46pm UTC](https://forums.powershell.org/t/delete-registry-key-value-if-it-exists/18158/5 "2021-12-15T16:46:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [December 15, 2021, 5:08pm UTC](https://forums.powershell.org/t/delete-registry-key-value-if-it-exists/18158/6 "2021-12-15T17:08:41Z")

</div>

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:

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

```

Edit: remove hyphen from `-SilentlyContinue`

---

<div class="post-metadata">

**Author:** ![mdtuser](https://avatars.discourse-cdn.com/v4/letter/m/3bc359/32.png) [@mdtuser](https://forums.powershell.org/u/mdtuser)\
**Post date:** [December 15, 2021, 5:16pm UTC](https://forums.powershell.org/t/delete-registry-key-value-if-it-exists/18158/7 "2021-12-15T17:16:15Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![mdtuser](https://avatars.discourse-cdn.com/v4/letter/m/3bc359/32.png) [@mdtuser](https://forums.powershell.org/u/mdtuser)\
**Post date:** [December 15, 2021, 5:27pm UTC](https://forums.powershell.org/t/delete-registry-key-value-if-it-exists/18158/8 "2021-12-15T17:27:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mdtuser](https://avatars.discourse-cdn.com/v4/letter/m/3bc359/32.png) [@mdtuser](https://forums.powershell.org/u/mdtuser)\
**Post date:** [December 15, 2021, 6:23pm UTC](https://forums.powershell.org/t/delete-registry-key-value-if-it-exists/18158/9 "2021-12-15T18:23:17Z")

</div>

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.’

---

<div class="post-metadata">

**Author:** ![mdtuser](https://avatars.discourse-cdn.com/v4/letter/m/3bc359/32.png) [@mdtuser](https://forums.powershell.org/u/mdtuser)\
**Post date:** [December 15, 2021, 6:24pm UTC](https://forums.powershell.org/t/delete-registry-key-value-if-it-exists/18158/10 "2021-12-15T18:24:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [December 15, 2021, 6:25pm UTC](https://forums.powershell.org/t/delete-registry-key-value-if-it-exists/18158/11 "2021-12-15T18:25:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mdtuser](https://avatars.discourse-cdn.com/v4/letter/m/3bc359/32.png) [@mdtuser](https://forums.powershell.org/u/mdtuser)\
**Post date:** [December 15, 2021, 6:27pm UTC](https://forums.powershell.org/t/delete-registry-key-value-if-it-exists/18158/12 "2021-12-15T18:27:17Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [December 15, 2021, 6:31pm UTC](https://forums.powershell.org/t/delete-registry-key-value-if-it-exists/18158/13 "2021-12-15T18:31:47Z")

</div>

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.

```auto
$WarningPreference = 'SilentlyContinue'

```

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

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

```

---

<div class="post-metadata">

**Author:** ![mdtuser](https://avatars.discourse-cdn.com/v4/letter/m/3bc359/32.png) [@mdtuser](https://forums.powershell.org/u/mdtuser)\
**Post date:** [December 15, 2021, 6:33pm UTC](https://forums.powershell.org/t/delete-registry-key-value-if-it-exists/18158/14 "2021-12-15T18:33:55Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [December 15, 2021, 6:39pm UTC](https://forums.powershell.org/t/delete-registry-key-value-if-it-exists/18158/15 "2021-12-15T18:39:16Z")

</div>

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![mdtuser](https://avatars.discourse-cdn.com/v4/letter/m/3bc359/32.png) [@mdtuser](https://forums.powershell.org/u/mdtuser)\
**Post date:** [December 15, 2021, 6:42pm UTC](https://forums.powershell.org/t/delete-registry-key-value-if-it-exists/18158/16 "2021-12-15T18:42:52Z")

</div>

```auto
$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.
```

---

<div class="post-metadata">

**Author:** ![mdtuser](https://avatars.discourse-cdn.com/v4/letter/m/3bc359/32.png) [@mdtuser](https://forums.powershell.org/u/mdtuser)\
**Post date:** [December 15, 2021, 6:44pm UTC](https://forums.powershell.org/t/delete-registry-key-value-if-it-exists/18158/17 "2021-12-15T18:44:25Z")

</div>

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.

```auto
$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

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [December 15, 2021, 9:10pm UTC](https://forums.powershell.org/t/delete-registry-key-value-if-it-exists/18158/18 "2021-12-15T21:10:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [December 15, 2021, 10:06pm UTC](https://forums.powershell.org/t/delete-registry-key-value-if-it-exists/18158/19 "2021-12-15T22:06:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:19pm UTC](https://forums.powershell.org/t/delete-registry-key-value-if-it-exists/18158/20 "2024-05-16T20:19:59Z")

</div>


