How to create a registry entry with a forward slash in the name How to create a registry entry with a forward slash in the name powershell powershell

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


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).


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.


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)