user2680126
user2680126

Reputation: 301

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

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

Upvotes: 18

Views: 13593

Answers (6)

Heredia
Heredia

Reputation: 1

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)

Upvotes: 0

harr2969
harr2969

Reputation: 31

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.

Upvotes: 3

Syed Sajjad
Syed Sajjad

Reputation: 1

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)

Upvotes: 0

JohnD
JohnD

Reputation: 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 /"

Upvotes: -1

Adi Inbar
Adi Inbar

Reputation: 12321

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.

Upvotes: 3

Keith Hill
Keith Hill

Reputation: 202072

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

Upvotes: 16

Related Questions