Error retrieving registry value with Powershell

后端 未结 3 1818
挽巷
挽巷 2020-12-18 10:15

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 th

相关标签:
3条回答
  • 2020-12-18 10:41

    Another version:

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

    0 讨论(0)
  • 2020-12-18 10:44

    Use Get-Item to get an object representing the registry key:

    PS > $regKey = Get-Item HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\IniFileMapping\Autorun.inf
    

    This gives you an instance of RegistryKey. RegistryKey has a method named GetValue; if the argument to GetValue is the empty string (''), then it will return the (default) value:

    PS > $regKey.GetValue('')
    

    Why is this better than Get-ItemProperty? It extends more naturally to Get-ChildItem. Get-ChildItem will give you a list of RegistryKey objects. In my particular case, I wanted to list the install paths of the versions of Python installed on my machine:

    PS C:\> get-childitem HKLM:\SOFTWARE\Wow6432Node\Python\PythonCore\*\InstallPath `
    >> | foreach-object { $_.GetValue('') }
    C:\Python26\ArcGIS10.0\
    C:\Python\27\
    
    0 讨论(0)
  • 2020-12-18 11:00

    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;
    
    0 讨论(0)
提交回复
热议问题