New-Item递归注册表项

6 registry shell powershell command-line

使用PowerShell,您可以运行这样的命令

ni c:/foo/bar -type directory
Run Code Online (Sandbox Code Playgroud)

它将创造foobar在必要时创建.但是,如果你跑

ni hklm:software/classes/firefoxhtml/shell/edit/command -type directory
Run Code Online (Sandbox Code Playgroud)

所有键,但最后一个必须存在或将生成错误.PowerShell可以根据需要生成父密钥吗?

小智 12

我只是错过了-force参数

New-Item hklm:software/classes/firefoxhtml/shell/edit/command -Force
Run Code Online (Sandbox Code Playgroud)

-Force如果已经存在,使用也将删除密钥下的所有内容,因此更好的选择

if(!(Test-Path $path)){
    New-Item $path -Force;
}
Run Code Online (Sandbox Code Playgroud)