views:

845

answers:

2

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".

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:

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.

+4  A: 

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

$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

$default = $a.psobject.Properties["(default)"];
JaredPar
To add to this, in the original code the () instructed PowerShell to treat the text (in this case "Default") as a separate command. When you have properties that have unusual characters like that, you need to quote (and sometimes use $()) the value like JaredPar showed so that PowerShell does not interpret the value as an expression. Other characters to watch out for are $ . : ' " (there may be more, but that is all I can remember)
JasonMArcher
Using $setting.'(default)' to escape the parenthesis also works.
Emperor XLII
+2  A: 

Another version:

(Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\IniFileMapping\Autorun.inf').'(default)'

Shay Levy