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

powershellwindows-registry

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.

Best Answer

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.