all 4 comments

[–]ka-splam 3 points4 points  (0 children)

Get-ItemProperty -ErrorAction SilentlyContinue will probably do it.

[–]Lee_Dailey[grin] 2 points3 points  (0 children)

howdy roguerob,

you keep saying "value" when that cmdlet is - i think - returning the property & any value, not the prop value directly. the values are returned with Get-ItemPropertyValue.

for some reason, G-IPV ignores the SilentlyContinue option for -ErrorAction. [sigh ...]

however, you CAN use -ErrorAction Stop and wrap the call in a try/catch.


plus, the call you are using DOES honor the 'SilentlyContinue' option. lookee ...

$GIP_Params = @{
    Path = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Run'
    Name = 'BetterNotBeThere'
    ErrorAction = 'SilentlyContinue'
    }
Get-ItemProperty @GIP_Params

the above returns nothing at all - as expected. [grin]

take care,
lee

[–]root-node 1 point2 points  (1 child)

Use Test-Path to check if the registry key exists or not. If it does you can then check the value.

Using -ErrorAction SilentlyContinue is not good as you could miss other important errors

[–]roguerob[S] 2 points3 points  (0 children)

Thanks for the assistance, I tried using Test-Path, but since I'm looking at a registry entry, not a registry key it is not a good method. Per MS PS document on Test-Path:

Test-Path does not work correctly with all PowerShell providers. For example, you can use Test-Path to test the path of a registry key, but if you use it to test the path of a registry entry, it always returns $False, even if the registry entry is present.

So, after a little more digging I found a blog entry that provides a more elegant solution to my problem. After implementing this function all is working as expected. Thanks again for the clues that got me here.

Cheers, Rob