Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# PowerShell argument Process , +
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 20-Jun-11 9:59am
Edited 20-Jun-11 10:03am
v3
Comments
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: bad
good
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""")
  Permalink  
v2
Comments
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: bad
good
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' """);
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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

Solution 1

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

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 535
1 OriginalGriff 275
2 Shweta N Mishra 244
3 BillWoodruff 235
4 Deepu S Nair 230
0 OriginalGriff 6,168
1 Sergey Alexandrovich Kryukov 5,818
2 DamithSL 4,958
3 Manas Bhardwaj 4,539
4 Maciej Los 3,755


Advertise | Privacy | Mobile
Web01 | 2.8.1411019.1 | Last Updated 17 Apr 2012
Copyright © CodeProject, 1999-2014
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