Recursive New-Item registry keys

In PowerShell, you can run such a command

ni c:/foo/bar -type directory

and if necessary, create fooand bar. However, if you run

ni hklm:software/classes/firefoxhtml/shell/edit/command -type directory

all keys, but the latter must exist or an error is generated. Can PowerShell generate parent keys as needed?

+3
source share
1 answer

I just missed the option -force

New-Item hklm:software/classes/firefoxhtml/shell/edit/command -Force

Using -forcewill also delete everything under the key, if it already exists, so the best option would be

if(!(Test-Path $path)){
    New-Item $path -Force;
}
+6
source

All Articles