This is what worked:
$Path = 'HKLM:\SOFTWARE\WOW6432Node\mykey'
$Name = 'GUID'
Get-ItemProperty -Path $Path -Name $Name -ErrorAction Ignore
Remove-ItemProperty -Path $Path -Name $Name -ErrorAction Ignore
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I'm trying to delete a 'value' of a key in the registry IF it exists. I don't know the action to script if it doesn't exist.
I can run this and it does remove the value if its there, but errors out when the value is not there.
The PS I added to MDT is:
Get-ItemProperty -Path "HKLM:\SOFTWARE\WOW6432Node\Keyname -Name "GUID"
Remove-ItemProperty -Path "HKLM:\SOFTWARE\WOW6432Node\Keyname" -Name "GUID" -ErrorAction Ignore
I want to remove the GUID entry within the key, if it sees it. What is the syntax to just do nothing if it doesn't see it?
This is what worked:
$Path = 'HKLM:\SOFTWARE\WOW6432Node\mykey'
$Name = 'GUID'
Get-ItemProperty -Path $Path -Name $Name -ErrorAction Ignore
Remove-ItemProperty -Path $Path -Name $Name -ErrorAction Ignore
Hi, @Rick Someone
Thank you for posting in Microsoft Q&A forum.
You may replace your PS script with this:
function Test-RegistryValue {
param (
[parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]$Path,
[parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]$Value
)
try {
Get-ItemProperty -Path $Path -Name $Value -ErrorAction Stop | Out-Null
return $true
}
catch {
return $false
}
}
if (Test-RegistryValue -Path 'HKLM:\SOFTWARE\WOW6432Node\Keyname' -value 'GUID')
{Remove-ItemProperty -Path 'HKLM:\SOFTWARE\WOW6432Node\Keyname' -Name GUID}
If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
This worked without error:
$Path = 'HKLM:\SOFTWARE\WOW6432Node\mykey'
$Name = 'GUID'
Get-ItemProperty -Path $Path -Name $Name -ErrorAction Ignore
Remove-ItemProperty -Path $Path -Name $Name -ErrorAction Ignore