Kae Travis

PowerShell Get-ItemPropertyValue Error Is Not Suppressed

Posted on by in PowerShell

This is a quick blog post to highlight why the PowerShell Get-ItemPropertyValue error is not suppressed.

First, an explainer.  Consider we want to read a registry value under HKLM:\Software\Alkane called Test.  We can simply read it like so:

$value = Get-ItemPropertyValue -Path 'HKLM:\Software\Alkane' -Name Test -ErrorAction SilentlyContinue
write-host $value

But wait!  If the name ‘Test’ does not exist, you would expect the error action to silently handle the error and just return nothing?  But it doesn’t.  Get-ItemPropertyValue ignores ErrorAction and spits out an ugly error message:

Get-ItemPropertyValue : Property Test does not exist at path HKEY_LOCAL_MACHINE\Software\Alkane

Apparently it’s because this is deemed as a “terminating error” and terminating errors are not suppressed by ErrorAction.  So, a solution?  Write it like this instead:

$value = Get-ItemProperty -Path 'HKLM:\Software\Alkane' | Select -ExpandProperty Test -ErrorAction SilentlyContinue
write-host $value

There you go – short and sweet!

PowerShell Get-ItemPropertyValue Error Is Not Suppressed
PowerShell Get-ItemPropertyValue Error Is Not Suppressed

Leave a Reply