Ala*_*son 63 variables powershell registrykey
任何人都可以帮我拉取注册表项的值并将其放入PowerShell中的变量中吗?到目前为止,我已经使用了Get-ItemProperty,reg query虽然两者都会提取值,但两者都会添加额外的文本.我只需要注册表项中的字符串文本,只需要键中的字符串文本.我确信我可以创建一个函数去除额外的文本,但如果有什么变化(即reg键名称),它可能会影响这个.
And*_*ndi 96
$key = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion'
(Get-ItemProperty -Path $key -Name ProgramFilesDir).ProgramFilesDir
Run Code Online (Sandbox Code Playgroud)
我从来不喜欢这是如何实现这样的提供程序:/
基本上,它使每一个注册表值PSCustomObject与对象PsPath,PsParentPath,PsChildname,PSDrive和PSProvider属性,然后为它的实际值的属性.因此,即使您按名称要求项目,要获得其价值,您还必须再次使用该名称.
Ian*_*emp 23
这些答案中的任何一个都适用于值名称包含空格,点或PowerShell中保留的其他字符的情况.在这种情况下,您必须根据http://blog.danskingdom.com/accessing-powershell-variables-with-periods-in-their-name/将名称用双引号括起来- 例如:
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
Run Code Online (Sandbox Code Playgroud)
如果要访问14.0,12.0,11.0,15.0中的任何一个值,则接受的答案中的解决方案将无效 - 您将无法获得输出:
PS> (Get-ItemProperty Registry::HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\VisualStudio\SxS\VS7 -Name 15.0).15.0
PS>
Run Code Online (Sandbox Code Playgroud)
什么工作引用了值名称,为了安全起见,您可能应该这样做:
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>
Run Code Online (Sandbox Code Playgroud)
因此,接受的答案应该如下修改:
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>
Run Code Online (Sandbox Code Playgroud)
这适用于PowerShell 2.0到5.0(尽管您应该Get-ItemPropertyValue在v5中使用).
mkl*_*nt0 14
注意:以下所有解决方案都绕过了Ian Kemp的回答中描述的问题 - 当用作属性名称时,需要对某些值名称使用显式引用; 例如,.'15.0'- 因为值名称作为参数传递,属性访问通过变量发生; 例如,.$ValueName
Harry Martyrossian在他自己的回答中提到了这个
Get-ItemPropertyValuecmdlet是在Powershell v5中引入的,它解决了这个问题:
PS> Get-ItemPropertyValue 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion' 'ProgramFilesDir'
C:\Program Files
Run Code Online (Sandbox Code Playgroud)
PowerShell v4的替代品:
这是尝试保持效率,同时消除重复值名称的需要,但是,这仍然有点麻烦:
& { (Get-ItemProperty `
-LiteralPath HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion `
-Name $args `
).$args } 'ProgramFilesDir'
Run Code Online (Sandbox Code Playgroud)
通过使用脚本块,值名称可以作为参数传入一次,然后参数variable($args)可以在块内简单地使用两次.
或者,一个简单的辅助函数可以缓解疼痛:
function Get-RegValue([String] $KeyPath, [String] $ValueName) {
(Get-ItemProperty -LiteralPath $KeyPath -Name $ValueName).$ValueName
}
Run Code Online (Sandbox Code Playgroud)
我不确定这是否已被更改,或者它是否与您正在使用的PS版本有关,但使用Andy的示例,我可以删除-Name参数并仍然获取reg项的值:
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
163054 次 |
| 最近记录: |