Creating a registry key with path components through PowerShell

For an outdated application, I need to create a registry key with the name in the format c:/foo/bar/baz . (Note: slashes, not backslashes). To be clear: this is one key name with a slash, which otherwise looks like a path to Windows. Since I need to script this against a large number of servers, PowerShell seems to be a great option.

The problem is that I cannot figure out how to create a key in this format through PowerShell. New-Item -Path HKLM:\SOFTWARE\Some\Key -Name 'c:/foo/bar/baz' errors using PowerShell I use / as a path separator and cannot find the path HKLM:\Software\Some\Key\c:\foo\bar , which really does not exist (and should not). I cannot find another way (ab) to use New-Item to get what I want.

Is there something that I am missing, or should I refuse and just generate and load the dump registry in the old-fashioned way?

+7
source share
1 answer

You need to do two things. First you need to get a writable RegistryKey , otherwise you cannot change anything. Second, use the CreateSubKey method directly in the RegistryKey object.

 $writable = $true $key = (get-item HKLM:\).OpenSubKey("SOFTWARE", $writable).CreateSubKey("C:/test") $key.SetValue("Item 1", "Value 1") 

After creating the key, you use the resulting object to add values ​​to it.

+11
source

All Articles