How do I get the value of a registry key and ONLY the value using powershell

前端 未结 9 536
醉酒成梦
醉酒成梦 2020-12-13 11:52

Can anyone help me pull the value of a registry key and place it into a variable in PowerShell? So far I have used Get-ItemProperty and reg query

相关标签:
9条回答
  • 2020-12-13 12:22

    NONE of these answers work for situations where the value name contains spaces, dots, or other characters that are reserved in PowerShell. In that case you have to wrap the name in double quotes as per http://blog.danskingdom.com/accessing-powershell-variables-with-periods-in-their-name/ - for example:

    PS> Get-ItemProperty Registry::HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\VisualStudio\SxS\VS7
    
    14.0         : C:\Program Files (x86)\Microsoft Visual Studio 14.0\
    12.0         : C:\Program Files (x86)\Microsoft Visual Studio 12.0\
    11.0         : C:\Program Files (x86)\Microsoft Visual Studio 11.0\
    15.0         : C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\
    PSPath       : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\VisualStudio\SxS\V
                   S7
    PSParentPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\VisualStudio\SxS
    PSChildName  : VS7
    PSProvider   : Microsoft.PowerShell.Core\Registry
    

    If you want to access any of the 14.0, 12.0, 11.0, 15.0 values, the solution from the accepted answer will not work - you will get no output:

    PS> (Get-ItemProperty Registry::HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\VisualStudio\SxS\VS7 -Name 15.0).15.0
    PS>
    

    What does work is quoting the value name, which you should probably be doing anyway for safety:

    PS> (Get-ItemProperty "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\VisualStudio\SxS\VS7" -Name "15.0")."15.0"
    C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\
    PS> 
    

    Thus, the accepted answer should be modified as such:

    PS> $key = "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\VisualStudio\SxS\VS7"
    PS> $value = "15.0"
    PS> (Get-ItemProperty -Path $key -Name $value).$value
    C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\
    PS> 
    

    This works in PowerShell 2.0 through 5.0 (although you should probably be using Get-ItemPropertyValue in v5).

    0 讨论(0)
  • 2020-12-13 12:24

    Harry Martyrossian mentions in a comment on his own answer that the
    Get-ItemPropertyValue cmdlet was introduced in Powershell v5, which solves the problem:

    PS> Get-ItemPropertyValue 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion' 'ProgramFilesDir'
    C:\Program Files
    

    Alternatives for PowerShell v4-:

    Here's an attempt to retain the efficiency while eliminating the need for repetition of the value name, which, however, is still a little cumbersome:

    & { (Get-ItemProperty `
          -LiteralPath HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion `
          -Name $args `
        ).$args } 'ProgramFilesDir'
    

    By using a script block, the value name can be passed in once as a parameter, and the parameter variable ($args) can then simply be used twice inside the block.

    Alternatively, a simple helper function can ease the pain:

    function Get-RegValue([String] $KeyPath, [String] $ValueName) {
      (Get-ItemProperty -LiteralPath $KeyPath -Name $ValueName).$ValueName
    }
    

    Note: All solutions above bypass the problem described in Ian Kemp's's answer - the need to use explicit quoting for certain value names when used as property names; e.g., .'15.0' - because the value names are passed as parameters and property access happens via a variable; e.g., .$ValueName


    As for the other answers:

    • Andy Arismendi's helpful answer explains the annoyance with having to repeat the value name in order to get the value data efficiently.
    • M Jeremy Carter's helpful answer is more convenient, but can be a performance pitfall for keys with a large number of values, because an object with a large number of properties must be constructed.
    0 讨论(0)
  • 2020-12-13 12:30

    Given a key \SQL with two properties:

    I'd grab the "MSSQLSERVER" one with the following in-cases where I wasn't sure what the property name was going to be to use dot-notation:

    $regkey_property_name = 'MSSQLSERVER'
    $regkey = get-item -Path 'HKLM:\Software\Microsoft\Microsoft SQL Server\Instance Names\SQL'
    $regkey.GetValue($regkey_property_name)
    
    0 讨论(0)
  • 2020-12-13 12:30

    Well you need to be specific here. As far as I know, the key in a registry is a "folder" of properties. So did you mean get the value of a property? If so, try something like this:

    (Get-ItemProperty HKLM:\Software\Microsoft\PowerShell\1\PowerShellEngine -Name PowerShellVersion).PowerShellVersion
    

    First we get an object containing the property we need with Get-ItemProperty and then we get the value of for the property we need from that object. That will return the value of the property as a string. The example above gives you the PS version for "legacy"/compatibility-mdoe powershell (1.0 or 2.0).

    0 讨论(0)
  • 2020-12-13 12:33

    I'm not sure if this has been changed, or if it has something to do with which version of PS you're using, but using Andy's example, I can remove the -Name parameter and I still get the value of the reg item:

    PS C:\> $key = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion'
    PS C:\> (Get-ItemProperty -Path $key).ProgramFilesDir
    C:\Program Files
    
    
    PS C:\> $psversiontable.psversion
    
    Major  Minor  Build  Revision
    -----  -----  -----  --------
    2      0      -1     -1
    
    0 讨论(0)
  • 2020-12-13 12:34

    Not sure at what version this capability arrived, but you can use something like this to return all the properties of multiple child registry entries in an array:

    $InstalledSoftware = Get-ChildItem "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" | ForEach-Object {Get-ItemProperty "Registry::$_"}
    

    Only adding this as Google brought me here for a relevant reason and I eventually came up with the above one-liner for dredging the registry.

    0 讨论(0)
提交回复
热议问题