How to suppress quotes in PowerShell commands to executables

12,393

Solution 1

Escape the inner quotes like this:

msiexec /a somepackage.msi TARGETDIR=`"c:\some path`" /qn

Solution 2

Here is a function I use to better handle multiple arguments and those with spaces and quotes. Note that to the code blocks below don't color where strings start and end correctly and you have to use ` to escape quotes you want in the parameter.

function InstallMSIClient{
$Arguments = @()
$Arguments += "/i"
$Arguments += "`"$InstallerFolder\$InstallerVerFolder\Install.msi`""
$Arguments += "RebootYesNo=`"No`""
$Arguments += "REBOOT=`"Suppress`""
$Arguments += "ALLUSERS=`"1`""
$Arguments += "/passive"

Write-Host "Installing $InstallerVerFolder."
Start-Process "msiexec.exe" -ArgumentList $Arguments -Wait }

There's a more complete example on my blog. [http://www.christowles.com]

Share:
12,393
David Gladfelter
Author by

David Gladfelter

Software Engineer in Test at Google in Boulder, CO.

Updated on June 04, 2022

Comments

  • David Gladfelter
    David Gladfelter almost 2 years

    Is there a way to suppress the enclosing quotation marks around each command-line argument that PowerShell likes to generate and then pass to external executables for command-line arguments that have spaces in them?

    Here's the situation:

    One way to unpack many installers is a command of the form:

    msiexec /a <packagename> /qn TARGETDIR="<path to folder with spaces>"
    

    Trying to execute this from PowerShell has proven quite difficult. PowerShell likes to enclose parameters with spaces in double-quotes. The following lines:

    msiexec /a somepackage.msi /qn 'TARGETDIR="c:\some path"'
    
    msiexec /a somepackage.msi /qn $('TARGETDIR="c:\some path"')
    
    $td = '"c:\some path"'
    
    msiexec /a somepackage.msi /qn TARGETDIR=$td
    

    All result in the following command line (as reported by the Win32 GetCommandLine() API):

    "msiexec" /a somepackage.msi /qn "TARGETDIR="c:\some path""
    

    This command line:

    msiexec /a somepackage.msi TARGETDIR="c:\some path" /qn
    

    results in

    "msiexec" /a fooinstaller.msi "TARGETDIR=c:\some path" /qn
    

    It seems that PowerShell likes to enclose the results of expressions meant to represent one argument in quotation marks when passing them to external executables. This works fine for most executables. However, MsiExec is very specific about the quoting rules it wants and won't accept any of the command lines PowerShell generates for paths have have spaces in them.

    Is there a way to suppress this behavior?

  • Jason R. Coombs
    Jason R. Coombs over 12 years
    Hooray! This works if you can include the string literally. What if you have it as a variable, i.e. $target_dir='c:\some path' ?
  • Jason R. Coombs
    Jason R. Coombs over 12 years
    I could not get this to work in a powershell script on Powershell 2.0. I tried the first technique (because I have my $installpath as a variable), but msiexec pops up the help indicating the parameters are invalid.
  • Jason R. Coombs
    Jason R. Coombs over 12 years
    This answer was immensely helpful. It led me to the answer for my own purposes, which was the only answer I could find which accepted MSI parameters from Powershell variables.
  • JNK
    JNK about 12 years
    @JasonR.Coombs then you would need to do Targetdir = "$target_dir"
  • Paweł Czopowik
    Paweł Czopowik almost 9 years
    Yes, this was extremely helpful. I had a parameter with multiple nested quotation marks which PS would just parse correctly.