Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start process with args which contains a path with blanks

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

like image 829
vklu4itesvet Avatar asked Jul 09 '13 14:07

vklu4itesvet


People also ask

How do I add a space in a PowerShell path?

In case you want to run powershell.exe -File from the command line, you always have to set paths with spaces in double quotes (""). Also try using the Grave Accent Character (`) PowerShell uses the grave accent (`) character as its escape character. Just add it before each space in the file name.

What is ArgumentList in PowerShell?

-ArgumentListSpecifies parameters or parameter values to use when this cmdlet starts the process. Arguments can be accepted as a single string with the arguments separated by spaces, or as an array of strings separated by commas.


2 Answers

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
like image 125
Aaron Jensen Avatar answered Sep 23 '22 09:09

Aaron Jensen


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.

like image 35
Benrobot Avatar answered Sep 25 '22 09:09

Benrobot