Click here to Skip to main content
11,922,320 members (39,893 online)
Rate this:
Please Sign up or sign in to vote.
This is my Script file. It displays all the arguments that are passed to it and their count.

Arguments: $($args.count)

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.

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.


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

It says there are 3 arguments.

Please help me here...!
Posted 20-Jun-11 9:59am
Edited 20-Jun-11 10:03am
SAKryukov 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"
Rate this: bad
Please Sign up or sign in to vote.

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""")
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)
Rate this: bad
Please Sign up or sign in to vote.

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: bad
Please Sign up or sign in to vote.

Solution 4

try following:

Process.Start(@"path\to\Powershell.exe", "&'@"-File ""ScriptwithArguments.ps1"" ""arg with space""'""")
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

If you Google it, you might find some ways to do it.
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)

  Print Answers RSS
0 OriginalGriff 493
1 Dave Kreskowiak 285
2 Sergey Alexandrovich Kryukov 243
3 Richard MacCutchan 155
4 ppolymorphe 135
0 OriginalGriff 7,231
1 KrunalRohit 4,530
2 Sergey Alexandrovich Kryukov 3,356
3 George Jonsson 2,865
4 Suvendu Shekhar Giri 2,216

Advertise | Privacy | Mobile
Web04 | 2.8.151120.1 | Last Updated 17 Apr 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100