n179911
n179911

Reputation: 20341

How can I start a process in PowerShell with arguments in quotes?

In my command line, I start my process like this:

ffmpeg -f dshow i video="Integrated Camera" c:\test.mp4

I want to do the same in PowerShell.

I tried:

$args = " -f dshow i video='Integrated Camera' c:\test.mp4"
Start-Process -FilePath ffmpeg.exe -ArgumentList $args

And I tried:

$args = " -f dshow i video=\"Integrated Camera\" c:\test.mp4"
Start-Process -FilePath ffmpeg.exe -ArgumentList $args

But in both cases, FFmpeg won't start.

How can I put " in my arguments?

Upvotes: 2

Views: 6507

Answers (1)

Mr Tree
Mr Tree

Reputation: 451

I would first urge caution that Start-Process may not be exactly what you desire. This will open a new process in a new command window (cmd).

I would encourage you to read the blog post PowerShell and external commands done right FYI.

However, there are a few ways to escape the quotes. One way to include double quotes in a string is to use a single quote to encapsulate the whole string for example:

$String = 'video="Integrated Camera"'

Alternatively you can escape with a backtick:

$String = "video=`"Integrated Camera`""

Another way would be to escape using double "s:

$String = "video=""Integrated Camera"""

The method you chose will be down to personal preference and the readability of your code. It's worth noting that the -ArgumentsList expects an array of strings.

Wrapping both of these up would give you something along the lines of:

$exe = "ffmpeg.exe"
$ffmpefArguments= "-f dshow i video=`"Integrated Camera`" c:\test.mp4"
&$exe $ffmpefArguments

Upvotes: 3

Related Questions