Rate this:
See more: , +
This is my Script file. It displays all the arguments that are passed to it and their count.

ScriptwithArguments.ps1
========================
Arguments: $($args.count)
\$args

I'm trying to run this script from C# Process.Start()

If I say
Process.Start("path\to\Powershell.exe",@"""ScriptwithArguments.ps1"" ""arg1"" ""arg2""")

it works fine. It says there are 2 arguments and displays them.
arg1
arg2

But when I try to execute the same script with arguments that have spaces in between, it takes those as different arguments even though I put them inside doublequotes.

Example:

Process.Start("path\to\Powershell.exe",@"""ScriptwithArguments.ps1"" ""arg with space""");

It says there are 3 arguments.
arg
with
space

Posted 20-Jun-11 9:59am
Edited 20-Jun-11 10:03am
v3
SAKryukov at 20-Jun-11 20:42pm

How does it work from the CMD.EXE shell?
What happens if you type:
"path\to\Powershell.exe" "ScriptwithArguments.ps1" "arg with space"
?
--SA

Rate this:

## Solution 2

Yes this is tricky but it seems a work a lot better if you use the -File named argument on the powershell command line.

In your case this would be

powershell -File "ScriptwithArguments.ps1" "arg with space"

which should resolve to

Process.Start(@"path\to\Powershell.exe", @"-File ""ScriptwithArguments.ps1"" ""arg with space""")
v2
deepthakannan at 22-Jun-11 8:53am

Thanks for looking into this !
deepthakannan at 22-Jun-11 9:00am

I tried yours and it works as long as there is no space in the path to the script file (ScriptwithArguments.ps1)
Rate this:

## Solution 3

I haven't tried Alan's suggestion. But I found a way to make this work.

Process.Start(@"path\to\Powershell.exe", @""" 'ScriptwithArguments.ps1' 'arg with space' 'more args' """);
Rate this:

## Solution 4

try following:

Process.Start(@"path\to\Powershell.exe", "&'@"-File ""ScriptwithArguments.ps1"" ""arg with space""'""")
Rate this:

## Solution 1

If you Google it, you might find some ways to do it.
Duker555 at 1-Apr-13 11:21am

if no one ever answers it then google searches would just be filled with worthless comments like yours