获取注册表值作为字符串的更简洁方法(Get-ItemProperty $ key $ valueName)._ VALUENAME_?

Jus*_*ing 14 registry powershell

从PowerShell获取注册表项中的值的方法是:

Get-ItemProperty HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion CommonFilesDir
Run Code Online (Sandbox Code Playgroud)

但是,该命令返回一些我通常不想要的额外属性:

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
Run Code Online (Sandbox Code Playgroud)

我只想要实际值,在这种情况下是一个字符串.要做到这一点,我必须使用更详细:

$commonFilesDir = (Get-ItemProperty HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion CommonFilesDir).CommonFilesDir
Run Code Online (Sandbox Code Playgroud)

除了编写我自己的别名之外,有没有办法不写两次属性名并获取字符串

我可以运行以下命令,但它返回一个PSObject:

Get-ItemProperty HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion | Select CommonFilesDir
Run Code Online (Sandbox Code Playgroud)

jla*_*ery 13

我是PowerShell的新手,但如果你在Get-ItemProperty中省略了注册表值名称,它似乎在PowerShell 2和3中工作,只使用值名称作为属性:

(Get-ItemProperty HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion).CommonFilesDir
Run Code Online (Sandbox Code Playgroud)

或者甚至更短的别名:

(gp HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion).CommonFilesDir
Run Code Online (Sandbox Code Playgroud)

没有重复的名称,清洁,它不能得到更简洁.