using REG ADD in PowerShell to add registry key with double quotes

3

I am trying to add registry keys to Windows 10 using a PowerShell script. The key in the registry must have double quotes included in the data field so I understand I must escape the double quote with a backslash.

The following example command throws a syntax error when executed in Powershell but works perfectly in a Command prompt window:

REG ADD \\COMPUTER1\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\dcpm-notify /v ImagePath /t REG_EXPAND_SZ /d "\"C:\Program Files\Dell\CommandPowerManager\NotifyService.exe\"" /f

I have tried changing the escape characters to ` and using """ etc but I cannot get any combination to work in a PowerShell.

Any suggestions greatly appreciated.

GOB

Posted 2016-12-07T18:01:26.330

Reputation: 33

1"The following example command throws a syntax error" - If you want help provide your error. – Ramhound – 2016-12-07T18:07:31.653

Why don't you just use the shortname for Program Files instead of dealing with a space, although your underline problem, is your syntax is wrong. You have unmatched escape characters "" through your directory string. You can also use %ProgramFiles% but resolve your directory problem first. – Ramhound – 2016-12-07T18:10:39.137

You can do something like $ProgramFiles = "${Env:ProgramFiles}" to make it simple – Ramhound – 2016-12-07T18:13:02.347

Thanks for your comments. The only error thrown is "Syntrax Error - check REG ADD /?" – GOB – 2016-12-07T18:18:02.513

Unfortunately this is a generic script to add double quotes to any unquoted paths with spaces. It's a security requirement. Not all will be Program files. – GOB – 2016-12-07T18:20:08.530

Have you confirmed you actually NEED to include those wrapping quotes? – Ƭᴇcʜιᴇ007 – 2016-12-07T18:24:55.127

@Techie007 yes afraid so. – GOB – 2016-12-07T18:27:51.613

Why not use single quotes around the text, so double quotes can be inserted normally? EG: 'my var with "quotes"' – LPChip – 2016-12-07T21:18:13.927

Answers

1

You can use [Microsoft.Win32.RegistryKey] to add the key.

For example:

$RemoteReg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine',$TargetComp)
$NewKey = $RemoteReg.OpenSubKey("SYSTEM\CurrentControlSet\Services\")
$NewKey.CreateSubKey('dcpm-notify`)
$NewValue = $RemoteReg.OpenSubKey("SYSTEM\CurrentControlSet\Services\dcpm-notify")
$NewValue.SetValue('ImagePath', 'C:\Program Files\Dell\CommandPowerManager\NotifyService.exe')

Where $TargetComp is a computer you want to edit the registry for.

Please note that I have not tested the exact code, but I have used something very similar to this in the past and works without any issues. So run this on a test system first if anything.

shinjijai

Posted 2016-12-07T18:01:26.330

Reputation: 1 391

ah, OK. So with this we could use the Powershell escape character ` to add double quotes to the value? I'll give that a try in the morning. – GOB – 2016-12-07T18:46:39.593

I managed to rewrite my scripts incorporating this method and it certainly go t me out of of a fix. Struggled getting it to work on remote registries but was able to overcome the need for that. – GOB – 2016-12-09T16:28:18.383

3

Since you're using PowerShell, I'd suggest using the New-Item and New-ItemProperty cmdlets instead of Reg.exe, as they will let you include the escaped quotes.

E.G:

$registryPath = "HKLM:\SYSTEM\CurrentControlSet\Services\dcpm-notify"
$name = "ImagePath"
$value = "`"C:\Program Files\Dell\CommandPowerManager\NotifyService.exe`""

# If registry path doesn't exist, create it.
If (-NOT (Test-Path $registryPath)) {
    New-Item $registryPath | Out-Null
}

New-ItemProperty -Path $registryPath `
    -Name $name `
    -Value $value `
    -PropertyType ExpandString `
    -Force | Out-Null

Note: This example is targeted at the local machine. To run it against remote computer(s), look into using the Invoke-Command PowerShell cmdlet to invoke the above commands on the remote computer(s).

Ƭᴇcʜιᴇ007

Posted 2016-12-07T18:01:26.330

Reputation: 103 763

0

The easiest answer would be to use single quotes around the text so double quotes become text by itself.

Your command would become:

REG ADD \\COMPUTER1\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\dcpm-notify /v ImagePath /t REG_EXPAND_SZ /d '"C:\Program Files\Dell\CommandPowerManager\NotifyService.exe"' /f

To explain it further:

Powershell knows two ways to work with text.

$test = "This is a test"
$test2 = 'This is also a test'

Because the above works, it allows you to do this:

$test3 = 'This is "double quoted" text'
$test4 = "This is 'single quoted' text"

And if you need to have a string which has both, you can accomplish it as follows:

$test5 = 'This is "double quoted" and ' + "'single quoted' text in one"

LPChip

Posted 2016-12-07T18:01:26.330

Reputation: 42 190

I had thought this originally too, but testing revealed that the inner quotes are still removed from the string before being added to the registry. I think it has something to do with the way REG.exe handles the string. – Ƭᴇcʜιᴇ007 – 2016-12-08T13:38:57.997