Click here to Skip to main content
15,886,703 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

Please help me here...!
Posted
Updated 20-Jun-11 9:03am
v3
Comments
Sergey Alexandrovich Kryukov 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

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""")
 
Share this answer
 
v2
Comments
deepthakannan 22-Jun-11 8:53am    
Thanks for looking into this !
deepthakannan 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)
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' """);
 
Share this answer
 
try following:

Process.Start(@"path\to\Powershell.exe", "&'@"-File ""ScriptwithArguments.ps1"" ""arg with space""'""")
 
Share this answer
 
If you Google it, you might find some ways to do it.
 
Share this answer
 
Comments
Duker555 1-Apr-13 11:21am    
if no one ever answers it then google searches would just be filled with worthless comments like yours

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900