Start process with args which contains a path with blanks

62,405

Solution 1

I think you can use Start-Process:

Start-Process -FilePath $setupFilePath -ArgumentList '-a','-s','-f1"d:\some directory\with blanks in a path\fileVCCS.iss"' |
    Wait-Process

Solution 2

I understand your question to be: How to pass multiple arguments to start a process where one of the arguments has spaces?

I'm assuming the equivalent in a Windows batch file would be something like:

"%setupFilePath%" -a -s -f1"d:\some directory\with blanks in a path\fileVCCS.iss"

where the double quotes allow the receiving process (setupFilePath in this case) to receive three arguments:

  1. -a
  2. -s
  3. -f1"d:\some directory\with blanks in a path\fileVCCS.iss"

To accomplish this with the code snippet in your question I would use back ticks (to the left of the 1 and below the escape key, not to be confused with a single quote; aka Grave-accent) to escape the inner double quotes like this:

$process = [System.Diagnostics.Process]::Start("$setupFilePath", "-a -s -f1`"d:\some directory\with blanks in a path\fileVCCS.iss`"") 
$process.WaitForExit()

Note that in addition to using back ticks I also changed the single quotes around your argument list to double quotes. This was necessary because single quotes do not allow the escapes we need here (http://ss64.com/ps/syntax-esc.html).

Aaron's answer should work just fine. If it doesn't then I would guess that setupFilePath is not interpretting -f1"d:\space here\file.ext" as you expect it to.

OPINION ALERT The only thing I would add to his answer is to suggest using double quotes and back ticks in order to allow using a variable within the path for argument -f1:

Start-Process -FilePath $setupFilePath -ArgumentList '-a','-s',"-f1`"$pathToVCCS`"" |
Wait-Process

This way you won't have a hard-coded, absolute path in the middle of a long line.

Solution 3

On PowerShell v3, this works:

& $setupFilePath -a -s -f1:"d:\some directory\with blanks in a path\fileVCCS.iss"

Using the PSCX echoargs command shows this:

25> echoargs.exe -a -s -f1"d:\some directory\with blanks in a path\fileVCCS.iss"
Arg 0 is <-a>
Arg 1 is <-s>
Arg 2 is <-f1d:\some directory\with blanks in a path\fileVCCS.iss>

Command line:
"C:\Program Files (x86)\PowerShell Community Extensions\Pscx3\Pscx\Apps\EchoArgs.exe"  -a -s "-f1d:\some directory\with blanks in a path\fileVCCS.iss"

On V2 use - note the addition of a backtick on the last double quote:

PS> echoargs.exe -a -s -f1"d:\some directory\with blanks in a path\fileVCCS.iss`"
Arg 0 is <-a>
Arg 1 is <-s>
Arg 2 is <-f1d:\some directory\with blanks in a path\fileVCCS.iss>

Command line:
"C:\Program Files (x86)\PowerShell Community Extensions\Pscx3\Pscx\Apps\EchoArgs.exe"  -a -s -f1"d:\some directory\with blanks in a path\fileVCCS.iss"
Share:
62,405
vklu4itesvet
Author by

vklu4itesvet

Updated on July 09, 2022

Comments

  • vklu4itesvet
    vklu4itesvet almost 2 years

    I need to start a process from a powershell script and pass such params : -a -s f1d:\some directory\with blanks in a path\file.iss to do that, I write the folowing code :

    $process = [System.Diagnostics.Process]::Start("$setupFilePath", '-a -s -f1"d:\some directory\with blanks in a path\fileVCCS.iss"') 
    $process.WaitForExit()
    

    as a result the process starts but the last argument : -f1d:\some directory\with blanks in a path\file.iss is not passing correctly. Help, please

  • vklu4itesvet
    vklu4itesvet almost 11 years
    The code above does not work in my case. It works only if a param '-f1"d:\some directory\with blanks in a path\fileVCCS.iss"' contains a path with no blanks
  • Keith Hill
    Keith Hill almost 11 years
    If you need it to work on 2.0, put a backtick ` before the last double quote.
  • Aaron Jensen
    Aaron Jensen almost 11 years
    Can you update your question with the error message you're getting?
  • vklu4itesvet
    vklu4itesvet almost 11 years
    actually there are no error message. I determine than it does not work as I want, because if launching $setupFilePath executable with incorrect path it works but in other way then expected. I also tried to pass a proper path(with no blanks) and it worked as I want
  • vklu4itesvet
    vklu4itesvet almost 11 years
    I am using powershell v1 if it has some influence
  • Aaron Jensen
    Aaron Jensen almost 11 years
    That could have something to do with it. Try using Process Monitor to figure out how Start-Process is sending the arguments to $setupFilePath. There maybe some kind argument pre-processing going on.