通过 PowerShell 创建带有路径组件的注册表项 [英] Creating a registry key with path components via PowerShell
问题描述
对于遗留应用程序,我需要创建一个名称为 c:/foo/bar/baz
格式的注册表项.(注意:正斜杠,而不是反斜杠.)需要明确的是:这是单个键的名称,带有正斜杠,否则看起来像 Windows 路径.因为我需要针对许多服务器编写脚本,所以 PowerShell 似乎是一个不错的选择.
For a legacy application, I need to create a registry key with a name in the format c:/foo/bar/baz
. (Note: forward slashes, not backslashes.) To be clear: that is a single key's name, with forward slashes, that otherwise looks like a Windows path. Because I need to script this against lots of servers, PowerShell seems like a great option.
问题是我无法弄清楚如何通过 PowerShell 创建该格式的密钥.New-Item -Path HKLM:\SOFTWARE\Some\Key -Name 'c:/foo/bar/baz'
错误,PowerShell 认为我使用 /
作为路径分隔符并且找不到路径 HKLM:\Software\Some\Key\c:\foo\bar
,它确实不存在(也不应该).我找不到任何其他方式 (ab) 使用 New-Item
来获得我想要的东西.
The problem is that I cannot figure out how to create a key in that format via PowerShell. New-Item -Path HKLM:\SOFTWARE\Some\Key -Name 'c:/foo/bar/baz'
errors out with PowerShell thinking I'm using /
as a path separator and failing to find the path HKLM:\Software\Some\Key\c:\foo\bar
, which does indeed not exist (and shouldn't). I can't find any other way to (ab)use New-Item
to get what I want.
是否有什么我遗漏的地方,或者我应该放弃并以老式的方式生成和加载注册表转储?
Is there something I'm missing, or should I give up and just generate and load a registry dump the old-fashioned way?
推荐答案
您需要做两件事.首先你需要得到一个可写的 RegistryKey
对象,否则你无论如何都不能修改任何东西.其次,直接在 RegistryKey
对象上使用 CreateSubKey
方法.
You need to do two things. First you need to get a writable RegistryKey
object, otherwise you can't modify anything anyway. Second, use the CreateSubKey
method on the RegistryKey
object directly.
$writable = $true
$key = (get-item HKLM:\).OpenSubKey("SOFTWARE", $writable).CreateSubKey("C:/test")
$key.SetValue("Item 1", "Value 1")
创建键后,您可以使用结果对象向其添加值.
After you create the key you use the resulting object to add values to it.
这篇关于通过 PowerShell 创建带有路径组件的注册表项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!