Harry Martyrossian mentions in a comment on [his own answer](
[To see links please register here]
) that the
**[Get-ItemPropertyValue](
[To see links please register here]
)** 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
}
---
<sup>Note: All solutions above *bypass* the problem described in [Ian Kemp's](
[To see links please register here]
)'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`</sup>
----------
As for the other answers:
* [Andy Arismendi's helpful answer](
[To see links please register here]
) explains the annoyance with having to _repeat_ the value name in order to get the value data _efficiently_.
* [M Jeremy Carter's helpful answer](
[To see links please register here]
) 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.