How to create a registry entry with a forward slash in the name
Asked Answered
H

6

18

I need to create the following registry entry HKLM:\software\bmc software\control-m/agent but am having a problem due to the forward slash before "agent"

I have no problem creating an entry that doesn't have the forward slash For example:

PS C:\powershell>  new-item -path 'HKLM:\software\bmc software\control-mXXXagent'

But creating with the forward slash fails.

PS C:\powershell>  new-item -path 'HKLM:\software\bmc software\control-m/agent'

New-Item : The registry key at the specified path does not exist. At line:1 char:10 + new-item <<<< -path 'HKLM:\software\bmc software\control-m/agent' + CategoryInfo : InvalidArgument: (HKEY_LOCAL_MACH...tware\control-m:String) [New-Item], ArgumentExceptio n + FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.NewItemCommand

And using the PowerShell backtic ` escape character doesn't help either.

PS C:\powershell>  new-item -path 'HKLM:\software\bmc software\control-m`/agent'

New-Item : The registry key at the specified path does not exist. At line:1 char:10 + new-item <<<< -path 'HKLM:\software\bmc software\control-m/agent' + CategoryInfo : InvalidArgument: (HKEY_LOCAL_MACH...ware\control-m:String) [New-Item], ArgumentExceptio n + FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.NewItemCommand

And advice would be appreciated. Thanks

Hardunn answered 13/8, 2013 at 20:42 Comment(7)
possible duplicate of Creating a registry key with path components via PowerShellIsbella
Can you create it manually? How bout saving the path as a string then using -path $string instead?Interstice
@Eris Oddly enough, there is no LiteralPath parameter on New-Item.Bushwa
It appears that it is indeed impossible with the current implementation of the registry provider. Ansgar's link is your best option.Aorangi
@Keith Hill A -LiteralPath parameter isn't needed for New-Item, because the -Path parameter doesn't use wildcards. For example, -Path 'HKLM:\software\bmc software\new.key*%$!@#' works fine. The only problem is with the forward slash. I thought it must be a bug, but apparently the real reason is that forward slash is interpreted as a path separator. New-Item -Path 'HKLM:/software\bmc software/newkey' would work fine, for example, to create a key called newkey. I'll update my answer to explain that.Oxyacetylene
@AdiInbar then the help topic is wrong (wouldn't be the first time). It says that Path accepts wildcards.Bushwa
@Keith Hill Ha, you're right! Wildcards don't even make sense in the -Path argument for New-Item. What would that do, bulk-create subkeys of the same name in each key that matches? That seems like a silly and dangerous feature and I'm glad it's not really there. But yes, the help says "Specifies the path to the location of the new item. Wildcards are permitted." I did actually test creating keys with asterisks and question marks, and they're interpreted literally - everything except slashes is, as far as I can tell. Must have been a copy pasta error.Oxyacetylene
B
16

This is a slight modification of the post that Ansgar pointed to:

new-item -path 'HKLM:\software\bmc software'
$key = (get-item HKLM:\).OpenSubKey("SOFTWARE\bmc software", $true)
$key.CreateSubKey('control-m/agent')
$key.Close()

This creates the key using the actual / char (0x2F).

Bushwa answered 15/8, 2013 at 19:10 Comment(2)
To add 'control-m/agent' as a variable use: $key.CreateSubKey($keyVar) with no tick marksWoolley
The answer that keeps on giving! Thanks @Keith !Trustworthy
B
3

Here's my improvement with the approach in two lines:

$Path = 'SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Ciphers'
    (Get-Item HKLM:).OpenSubKey($path,$true).CreateSubKey("DES 56/56")

If you need to create an item under that key next, (e.g. to disable weak cryptography) you can use something like this, because new-itemproperty works fine with the forward slash. Note this requires the same $path variable and format I just shared above.

New-ItemProperty -Path "HKLM:\$Path\DES 56/56" -PropertyType DWORD -Value '0' -Name 'Enabled' -Force

Here's an outline of the problems:

  1. The "New-item" method does not seem to work with any approach that includes a forward slash e.g. "/" because that represents a sub-key. So "40/128" turns into "40" with a sub key of "128" Don't waste your time on this as of PowerShell v5.1 / July 2021.
  2. The forward slash cannot apparently be escaped at all in the "new-item" command. Using single quotes or double quotes or backslashes doesn't work.
  3. There are at least two ascii symbols that look similar to the forward slash but are not the same. The "real" code is [char]0x002F, not [char]0x2215. Fortunately, this approach doesn't need codes.
Bolinger answered 2/8, 2021 at 16:16 Comment(1)
A more generic approach would be: if($MyPsRegKeyPath -match '^(.*)\\(.*)\\(.*)$') { (Get-Item -Path $Matches[1]).OpenSubKey($Matches[2],$true).CreateSubKey($Matches[3]) }Ossian
O
2

Any printable character except \ is valid in the name of a registry key, but the reason the forward slash doesn't work in registry paths is that PowerShell accepts forward slashes as path separators. So, New-Item -Path 'HKLM:\software\bmc software\control-m/agent' is the same as New-Item -Path 'HKLM:\software\bmc software\control-m\agent', i.e. it attempts to add a key called agent to HKLM:\software\bmc software\control-m, which doesn't exist.

You have several options to get around this.

If you want just want something that looks like a forward slash and it's not important to have a true ASCII forward slash character, the simplest thing you can do is substitute the unicode division slash. You can interpolate it into a double-quoted string like this:

New-Item -Path 'HKLM:\software\bmc software' -Name "control-m$([char]0x2215)agent"

(That also works if you put everything in the -Path argument, but it's probably a better habit to do it this way so you don't have to worry about special characters in the rest of the path.)

If it needs to be an ASCII forward slash, you can use the method in the post linked by Ansgar Wiechers and elaborated on by Keith Hill, or you can use .NET to create the subkey:

([Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]::LocalMachine, $env:COMPUTERNAME)).CreateSubKey('Software\bmc software\control-m/agent')
  • The first parameter of the OpenRemoteBaseKey method specifies the registry hive. For a key in HKCU, change LocalMachine to CurrentUser.
  • The second parameter specifies specifies the name of the computer whose registry will be accessed. You can specify a remote computer, if the Remote Registry service is running on that computer.
Oxyacetylene answered 15/8, 2013 at 0:2 Comment(3)
OK but this isn't the actual forward slash character. It is another unicode code point that looks similar.Bushwa
@KeithHill Good catch. I did a quick google search for "unicode forward slash" and grabbed an incorrect answer. Should've noticed that it was way too high to be in the normal ASCII range. It's actually a division slash. I'll leave that in the answer, because for some purposes it might be simpler to use a faux forward slash than deal with alternate methods for working with the registry, but I'll add another way to do it (with a real slash).Oxyacetylene
The .NET solution is a good idea. But using an unicode that "LOOKS LIKE a forward slash" doesn't really answer the question and is misleading for anyone who doesn't pay attention to this "minor detail". Why don't you rewrite this answer giving more emphasis on the .NET solution and striking-through the unicode hack or leaving it to the end?Bigamist
B
0

You might need to embed DOS command within your PowerShell.

$PathCMD = "HKEY_LOCAL_MACHINE\Software\BMC Software"
$command = 'cmd.exe /C reg.exe add "$PathCMD\control-m/agent"'
Invoke-Command -Command $ExecutionContext.InvokeCommand.NewScriptBlock($command)
Bruin answered 7/12, 2015 at 16:20 Comment(0)
L
0

Using, [char]0x2215, helped me in my efforts to get the forward slash to work with the Ciphers registry key creation.

$CipherName = $Cipher.Name.Replace("/",[char]0x2215)

Lumen answered 8/8, 2023 at 9:2 Comment(5)
0x2215 is the unicode "division slash" character which is slightly different from a typical forward slash. Just to confirm, setting the registry key with this still works as expected?Caboose
Yes, it worked out very well in my case, but I will try with [char]0x002F as @Bolinger mentioned.Lumen
@Caboose you where right, it was different and using [char]0027F threw my back to the original problem.Lumen
If you haven't already I would suggest you follow Keith's answer.Caboose
Thanks @RiverHeart! Since I can't provide a longer example due to character limitations, I can't share the code but, Keith's answer acted like a template and it solved my problem. :) And don't hesitate to contact me, should you want the code. $keyPath = 'HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Ciphers\AES 256/256' $key = (Get-Item HKLM:\).OpenSubKey('SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Ciphers', $true).CreateSubKey('AES 256/256') $key.SetValue('Enabled', 1, 'DWord') $key.Close()Lumen
C
-1

Detailed below is an example of how you can string together registry entries including a forward slash:

$value = "2048"
$value1 = "0"
$regpath = "hklm:\SYSTEM\CurrentControlSet\Services\lanmanworkstation\parameters"
$name = "MaxCmds"
$name1 = "RequireSecuritySignature"
$PropertyType = "Dword"    
New-ItemProperty -path $regpath -name $name -value $value -PropertyType $PropertyType 
Set-ItemProperty -path $regpath -name $name1 -value $value1 

So for your requirement do the following:

$name1 = "something with a /"
Cavalla answered 14/8, 2013 at 15:45 Comment(1)
The OP is asking how to create a RegKey, not a Property.Aorangi

© 2022 - 2025 — McMap. All rights reserved.