1

How do I add into the registry in which the value of the UninstallString needs to have quotation marks at the beginning and end.

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Microsoft Visual Studio 2010 Tools for Office Runtime (x64)

enter image description here

tried to edit the value of the registry with this but received an error

New-ItemProperty -Path "HKLM:\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Microsoft Visual Studio 2010 Tools for Office Runtime (x64)" -Name "UninstallString" -Value ""C:\Program Files\Common Files\Microsoft Shared\VSTO\10.0\Microsoft Visual Studio 2010 Tools for Office Runtime (x64)\install.exe"" -PropertyType "String"

enter image description here

mklement0
  • 382,024
  • 64
  • 607
  • 775
KyleXY
  • 41
  • 3

1 Answers1

1

Your question boils down to this: how can I pass an argument that has embedded " characters, i.e. " characters that are a verbatim part of the argument?

Specifically, you want New-ItemProperty's -Value parameter to see the following value verbatim:

"C:\Program Files\Common Files\Microsoft Shared\VSTO\10.0\Microsoft Visual Studio 2010 Tools for Office Runtime (x64)\install.exe"

The tl;dr solution, using '...' quoting, is:

New-ItemProperty `
  -Path "HKLM:\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Microsoft Visual Studio 2010 Tools for Office Runtime (x64)" -Name "UninstallString" `
  -Value '"C:\Program Files\Common Files\Microsoft Shared\VSTO\10.0\Microsoft Visual Studio 2010 Tools for Office Runtime (x64)\install.exe"' `
  -PropertyType String

Your own attempt - passing ""C:\Program Files\..."" - was flawed in that the initial "" created an empty-string argument, followed by argument C:\Program, and so on - which isn't what you intendded.

Read on for a overview of all solution options.


The following is a systematic overview of the solution options; it uses Write-Output and verbatim value "foo bar" for brevity, which enables easy verification of whether the argument was passed as intended:

Write-Output '"foo bar"'
  • Use an expandable (double-quoted) string ("..."), if the value must be derived from variable values and/or subexpressions:
    • Inside "...", embedded " chars. must be escaped; while doubling them works (""), the preferable solution for consistency is to use `", because it uses PowerShell's general-purpose escape character, `, the so-called backtick:
$var = 'foo'
Write-Output "`"$var bar`""

Alternatively, use the here-string variants of the string-literal forms shown above, whose syntax is invariably multiline, and which are especially useful for defining multiline strings in a readable format; however, they are also useful for defining single-line strings, because they avoid the need for escaping of embedded quotes:

  • Verbatim (single-quoted) here-string:

    • Note: Since " do not require escaping in regular '...' strings anyway, there's not much benefit to using the here-string form in this case.
    • However, if the string contained embedded ' chars., you would benefit from not having to escape them (in regular verbatim strings, you'd have to escape them as '').
Write-Output @'
"foo bar"
'@ # IMPORTANT: Closing delimiter must be at the VERY START OF THE LINE
  • Expandable (double-quoted) here-string:

    • Note how the embedded " need no escaping in this case.
    • The only character that potentially needs escaping - with ` - is $, namely if you do not want it to be considered the start of variable reference (e.g,. $foo) or subexpression (e.g, $(1 + 2)) to be expanded.
$var = 'foo'
Write-Output @"
"$var bar"
"@ # IMPORTANT: Closing delimiter must be at the VERY START OF THE LINE

Caveat re calling external programs:

  • Up to PowerShell 7.2.x, the sad reality is that an extra, manual layer of \-escaping of embedded " characters is required.

  • Since PowerShell 7.3.0, this is now mostly no longer necessary, but on Windows there are selective exceptions:

mklement0
  • 382,024
  • 64
  • 607
  • 775