Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error retrieving registry value with Powershell

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.

like image 782
MattH Avatar asked Dec 05 '22 06:12

MattH


1 Answers

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)"].Value;
like image 113
JaredPar Avatar answered Dec 07 '22 20:12

JaredPar