When I try to use the comparison operator when reading the registry, the (default) keys don't seem to compare correctly. The default keys are string, but when I compare to string, it doesn't match:
First, example registry created:
If we then run the following command:
(gp HKCU:\branch1\branch2).PSObject.Properties | ? {$_.Name -notlike 'PS*'} | select Name, TypeNameOfValue, Value | ft -a
Result:
Name TypeNameOfValue Value
---- --------------- -----
(default) System.String defaultValue
testVal1 System.String testValue
As you can see, it clearly lists the default value as a String. So, why does the following comparison NOT work?
Get-ItemProperty "HKCU:\branch1\branch2" | ForEach-Object {
$CurrentKey = $_
write-host "Looking at key $CurrentKey"
foreach ($PropertyName in ($CurrentKey | Get-Item).GetValueNames()) {
if ($CurrentKey.$PropertyName -is [string]) {
write-host $PropertyName with value of $CurrentKey.$PropertyName STRING!
}
else{
write-host $PropertyName with value of $CurrentKey.$PropertyName NOT STRING!
}
}
}
Which gives the result:
Looking at key @{(default)=defaultValue; testVal1=testValue}
with value of NOT STRING!
testVal1 with value of testValue STRING!
You will see the (default) value output is blank (both the name and the value) and is not detected as a string. Why is this? Why can't I compare the default value to a STRING?
I've created a test key with the teststring value. This how I compared the TypeName of the value :
$test = (gp HKCU:\test\).PSObject.Properties
if (($test.Item("teststring").TypeNameOfValue) -eq "System.String") {"yes"} else {"no"}