More succinct way of getting a registry value as a string than (Get-ItemProperty $key $valueName)._VALUENAME_?

powershell, registry

Solution

I'm new to PowerShell, but it seems to work in PowerShell 2 and 3 if you leave out the registry value name in Get-ItemProperty, using the value name only as a property:

(Get-ItemProperty HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion).CommonFilesDir

or even shorter with the alias:

(gp HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion).CommonFilesDir

No repetition of the value name, clean, and it can't get much more succinct.

Problem

The method for getting a value in a registry key from PowerShell is: ``` Get-ItemProperty HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion CommonFilesDir ``` However, that command returns some extra properties I don't usually want: ``` CommonFilesDir : C:\Program Files\Common Files PSPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion PSParentPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows PSChildName : CurrentVersion PSDrive : HKLM PSProvider : Microsoft.PowerShell.Core\Registry ``` I just want the actual value, a string in this case. To do that I have to use the more verbose: ``` $commonFilesDir = (Get-ItemProperty HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion CommonFilesDir).CommonFilesDir ``` Other than writing my own alias, is there a way of not writing the property name twice and getting a string? I could run the following command, but it returns a PSObject: ``` Get-ItemProperty HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion | Select CommonFilesDir ```

Original source