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.
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.
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.
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:
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 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:
$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.