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:
Write-Output @'
"foo bar"
'@ # IMPORTANT: Closing delimiter must be at the VERY START OF THE LINE
$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: