4

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
David Gladfelter
  • 4,175
  • 2
  • 25
  • 25

6 Answers6

8

Escape the inner quotes like this:

msiexec /a somepackage.msi TARGETDIR=`"c:\some path`" /qn
stej
  • 28,745
  • 11
  • 71
  • 104
6

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]

Chris Towles
  • 101
  • 2
  • 4
  • 1
    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. – Jason R. Coombs Aug 20 '11 at 21:25
  • Yes, this was extremely helpful. I had a parameter with multiple nested quotation marks which PS would just parse correctly. – Paweł Czopowik Jun 25 '15 at 16:28
0

Put the entire argument in quotes and escape the inner quotes. Otherwise PowerShell will try to parse it:

msiexec /a <packagename> /qn 'TARGETDIR=\"<path to folder with spaces>\"'
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
zdan
  • 28,667
  • 7
  • 60
  • 71
0

I don't have the answer, but this guy seems to be onto something.
http://www.eggheadcafe.com/software/aspnet/33777311/problem-escaping-command.aspx

I couldn't make it work for me.

Here's someone else reporting the issue too: _http://powershell.com/cs/forums/p/2809/3751.aspx

Here's another idea by someone: _http://www.roelvanlisdonk.nl/?p=1135

That didn't work for me either...

Elijah W. Gagne
  • 2,801
  • 4
  • 31
  • 29
0

I don't have the answer, but this guy seems to be onto something. http://www.eggheadcafe.com/software/aspnet/33777311/problem-escaping-command.aspx

Yeah, it looks like they found a solution at the end:

Finally worked out how to do this using invoke-expression:

$installprop = "TARGETDIR=" + "```"" + $installpath + "```""

invoke-expression "msiexec /i $packagepath $installprop"

I would recommend using a here-string though to avoid having to do all the escaping.

$command = @'
msiexec /a <packagename> /qn TARGETDIR="<path to folder with spaces>"
'@

invoke-expression $command
Yanagi
  • 43
  • 1
  • 1
  • 8
  • 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 Aug 20 '11 at 21:06
0

I've just hit the problem. The following worked for me:

&cmd /c "msiexec /i `"$appName.msi`" /l* `"$appName.msi.log`" /quiet TARGETDIR=`"D:\Program Files (x86)\$appName\`""

The key was executing cmd rather than msiexec directly. This had two benefits:

  1. I could wrap the entire msiexec command in a string, so it would resolve the the $appName variable while still using the backtick to escape the quotes around TARGETDIR.
  2. I was able to make use of $LastExitCode to check success/failure of the msiexec operation. For some reason $LastExitCode isn't set when calling msiexec directly (hat-tip: http://www.powergui.org/thread.jspa?threadID=13022)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
DGreen
  • 1,027
  • 1
  • 8
  • 17