Question:
I’m attempting to read a value from a registry entry with Powershell. This is fairly simple, however, one particular registry key is giving me trouble.
If I run the following, I can’t get the value of the (default) of “$setting”.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
C:\Program Files\PowerGUI> $setting = Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\IniFileMapping\Autorun.inf" C:\Program Files\PowerGUI> $setting PSPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\ CurrentVersion\IniFileMapping\Autorun.inf PSParentPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\ CurrentVersion\IniFileMapping PSChildName : Autorun.inf PSDrive : HKLM PSProvider : Microsoft.PowerShell.Core\Registry (default) : @SYS:DoesNotExist |
Normally, I would do $setting.Attribute, or $setting.(default). However, that results in the following error:
1 2 3 4 5 |
C:\Program Files\PowerGUI> $setting.(default) The term 'default' is not recognized as a cmdlet, function, operable program, or script file. Verify the term and try again. At :line:1 char:17 + $setting.(default <<<< ) |
How do I get the value of the “(default)” attribute?
Thanks in advance.
Answer:
EDIT Had to look through and old script to figure this out.
The trick is that you need to look inside the underlying PSObject to get the values. In particular look at the properties bag
1 2 3 |
$a = get-itemproperty -path "HKLM:\Some\Path" $default = $a.psobject.Properties | ?{ $_.Name -eq "(default)" } |
You can also just use an indexer instead of doing the filter trick
1 2 |
$default = $a.psobject.Properties["(default)"].Value; |