New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Get-ItemPropertyValue ignores ErrorAction SilentlyContinue if registry key does not exist #5906
Comments
As a 5.1 issue and yesterdays blog mentioning the Consider-WindowsPowerShell51 label is it worth adding that label to this issue? |
@SwarfegaGit This is a terminating error. Terminating errors are not suppressed by |
@markekraus so this is a non-terminating error? PS C:\Program Files\PowerShell\6.0.0> Get-ItemProperty -Path 'HKCU:\' -Name xyzzy -ErrorAction SilentlyContinue
PS C:\Program Files\PowerShell\6.0.0> Get-ItemProperty -Path 'HKCU:\' -Name xyzzy
Get-ItemProperty : Property xyzzy does not exist at path HKEY_CURRENT_USER\.
At line:1 char:1
+ Get-ItemProperty -Path 'HKCU:\' -Name xyzzy
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (xyzzy:String) [Get-ItemProperty], PSArgumentException
+ FullyQualifiedErrorId : System.Management.Automation.PSArgumentException,Microsoft.PowerShell.Commands.GetItemPropertyCommand Any reason it should differ? |
The difference being, IMO, that |
If we needed to make that distinction, the error record should give us the info that we need. It feels like there is a terminating error here when there does not need to be. As I take a look at this cmdlet, it is inconsistent in how it handles this. I can suppress a bad path, but not a bad property.
As a scripter, I expect to be able to suppress both without using try/catch. |
This seems like an oversight, and should not be a terminating error. We use try/catch to grab errors and report up the thread but this terminating takes control away from the coder. Anyways... I am getting around it in my case by using: Get-ItemProperty -Path $KeyPath | Select-Object $KeyName -ExpandProperty $KeyName Which will spit out the value if there is one and do a non-terminating if there is no value found. |
Great analysis, @KevinMarquette, and agreed, @CoreyChaplan: From https://docs.microsoft.com/en-us/powershell/developer/cmdlet/cmdlet-error-reporting:
Therefore, the error at hand should not be a terminating error, given that An aside re your workaround:
is sufficient - also including Depending on your strict-mode settings, even |
Thanks for @KevinMarquette and @CoreyChaplan for their observation. Here is a third angle from me: We already know that the following generates an error: Get-ItemPropertyValue -Path "C:\Windows" -Name "SomethingInvalid" -ErrorAction SilentlyContinue However, the following does not: $ErrorActionPreference="SilentlyContinue"
Get-ItemPropertyValue -Path "C:\Windows" -Name "SomethingInvalid"
$ErrorActionPreference="Continue" I am afraid I don't find the logic of our esteemed @markekraus applicable here. |
Re contrasting That is a fundamental (problematic) behavioral distinction in PowerShell: In the case at hand, The bottom line is: |
(Never mind) |
I was pointing out that you were referring to a fundamental problem with PowerShell's error handling and that, as such, it is incidental to the problem at hand - it applies whether or not use of a terminating error is justified. (If you want to learn about all related warts, read this).
I was pointing out that there is indeed a problem with From what I can tell, the only one not in agreement with this in this thread is @markekraus. |
@mklement0 Really? That's what you wanted to say? Alright. I'm fine with that. I apologize for misunderstanding it. And as a gesture of goodwill, I strike my original reply. But I emphasize that your original message did not sound like what you are writing now and still does not. You see, you took your time to write what I had already discovered on my own, and reported in a prior post. So, naturally, I'd assume your purpose was something besides informing me, i.e. going from a premise to a conclusion. |
This bug still exist in 2023, 5 years after initial report... |
@joakimlemb Do you want to pull PR with fix? |
Another example of why this should be considered a bug and fixed... The "old" way of reading a registry value, before Get-ItemPropertyValue was introduced,... (Get-ItemProperty -Path 'HKCU:').xyzzy Simply returns null if the registry value doesn't exist. It doesn't even generate an error. In my opinion then, Get-ItemPropertyValue should function similarly. At the very least we need to be able to use -ErrorAction SilentlyContinue with Get-ItemPropertyValue. That said, utilizing the old syntax is an easy workaround, once you know this issue exists. |
@zaphod4254 - the proper fix for the issue at hand notwithstanding - see also: |
I heartily agree the error should be non terminating. In my scenario, the error is thrown when I am trying to get a list of registry settings to show the user. I anticipated the possibility that some properties might not be set. My expectation was that -ErrorAction Ignore would make Get-ItemPropertyValue return a null object, which I would then convert to “Undefined” for the benefit of the user. But I don't have that option because the current implementation of Get-ItemPropertyValue won’t return a null if the property doesn’t exist. |
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you. |
This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes. |
This is still happening and is still 🤡 |
This seems to be a bug in Windows PowerShell 5.1 so I figured I would see if it was resolved in 6.0.0 but it's still there.
Steps to reproduce
Expected behavior
Actual behavior
Environment data
The text was updated successfully, but these errors were encountered: