Qoutes - Start-Process

There is one topic from my latest thread “error.count does not work” concerning calling batchfiles from wirhin Powershell where I got lost:

Start-Process -FilePath $Env:ComSpec -ArgumentList '/C', $batfile -Wait -WindowStyle Hidden

What if $batfile contains blanks? All my trials with qoutes did not work.

Thanks again - Michael

Please show what you mean.

Let $batfile be: c:\temp\install firefox.bat (one blank within filename; it works if filename has no blanks).
Neither of these commands did work (did start the batchfile):

Start-Process -FilePath $Env:ComSpec -ArgumentList '/C', $batfile -Wait -WindowStyle Hidden
Start-Process -FilePath $Env:ComSpec -ArgumentList '/C', '$batfile' -Wait -WindowStyle Hidden
Start-Process -FilePath $Env:ComSpec -ArgumentList '/C', "$batfile" -Wait -WindowStyle Hidden
Start-Process -FilePath $Env:ComSpec -ArgumentList '/C', '"$batfile"' -Wait -WindowStyle Hidden

What’s wrong?
Thanks - Michael

Yeah … quoting in PowerShell can be tricky sometimes. :man_shrugging:t4:

Since PowerShell removes double quotes while passing them to the called process and since CMD does not use single quotes you have to use double inside as well. And for that to work properly you have to escape the inner double quotes. Either with a backtick - what’s very ugly I think. Or simply with double double quotes like this:

Start-Process -FilePath $Env:ComSpec -ArgumentList '/C', """$batfile""" -NoNewWindow -Wait

… looks funny weird, but it works. :wink:

This works as well …

Start-Process -FilePath $Env:ComSpec -ArgumentList '/C', "`"$batfile`"" -NoNewWindow -Wait

… but since it looks asymetric I don’t like it. :-1:t3: :wink:

Thanks! Looks weird “but” works.

Michael