powershell

PowerShell / Start-Process with CMD / problem with spaces in path


​Hi, Trying to run PowerShell code via ISE. I wanted the cmdlet Start-Process pass the command to the CMD console. Everything works fine when the destination path and the new file name do not contain a blank space in the name, such as the code below.

Start-Process -FilePath cmd.exe -ArgumentList "/c", """C:\Program Files (x86)\Internet Download Manager\IDMan.exe""", "/p", "C:\Users\Andrzej\Desktop\qap", "/h", "/n", "/q", "/f", "foo.exe", "/d", "https://www.foobar2000.org/files/foobar2000-x64_v2.24.3.exe"

The problem is when the target path to save the file or the new defined name contains an empty space. I have tried adding additional quotation marks but this does not help

File name: foo bar.exe

Target path: C:\Users\Andrzej\Desktop\qap 22

Start-Process -FilePath cmd.exe -ArgumentList "/c", """C:\Program Files (x86)\Internet Download Manager\IDMan.exe""", "/p", """C:\Users\Andrzej\Desktop\qap 22""", "/h", "/n", "/q", "/f", """foo bar.exe""", "/d", "https://www.foobar2000.org/files/foobar2000-x64_v2.24.3.exe"

The problem is certainly the location of the quotation marks, unfortunately I cannot manage with this :(

I tried changing the quotes to something else and changing Start-Process to Echo to see what it displays, In the preview it looks good but does not work.

echo -FilePath cmd.exe -ArgumentList "/c", "`"C:\Program Files (x86)\Internet Download Manager\IDMan.exe`"", "/p", "`"C:\Users\Andrzej\Desktop\qap 22`"", "/h", "/n", "/q", "/f", "`"foo bar.exe`"", "/d", "https://www.foobar2000.org/files/foobar2000-x64_v2.24.3.exe"
-FilePath
cmd.exe
-ArgumentList
/c
"C:\Program Files (x86)\Internet Download Manager\IDMan.exe"
/p
"C:\Users\Andrzej\Desktop\qap 22"
/h
/n
/q
/f
"foo bar.exe"
/d
https://www.foobar2000.org/files/foobar2000-x64_v2.24.3.exe

Solution

  • Therefore (using a here-string to simplify embedded quoting; implied target parameter names -FilePath and -ArgumentList omitted):

    Start-Process "C:\Program Files (x86)\Internet Download Manager\IDMan.exe" @"
    /p "C:\Users\Andrzej\Desktop\qap 22" /h /n /q /f "foo bar.exe" /d "https://www.foobar2000.org/files/foobar2000-x64_v2.24.3.exe"
    "@
    

    Note that the double-quoted i.e. expandable (interpolating) form of a here-string is used above; however, if all of your arguments are passed literally, as in this case, rather than referencing variables, you can use a '...', i.e. verbatim string instead (whether or not in its regular ('...') or its here-string form (@'<newline>...<newline>'@).

    To demonstrate a solution based on variables (this is the all-arguments-in-a-single-string equivalent of the cmd.exe /c-free solution you later posted yourself):

    $idmPath = 'C:\Program Files (x86)\Internet Download Manager\IDMan.exe'
    $downloadPath = 'C:\Users\Andrzej\Desktop\qap 22'
    $fileName = 'foo bar.exe'
    $downloadURL = 'https://www.foobar2000.org/files/foobar2000-x64_v2.24.3.exe'
    
    Start-Process $idmPath @"
    /p "$downloadPath" /h /n /q /f "$fileName" /d "$downladUrl"
    "@
    

    Finally, given that you seem to want to invoke IDMan.exe synchronously, you can more simply use direct invocation as follows (rather than add -Wait to the Start-Process call), using &, the call operator:

    & $idmPath /p $downloadPath /h /n /q /f $fileName /d $downladUrl | Write-Output