• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 198
  • Last Modified:

Running powershell from cmd line with many parameters

Within powershell this works, but I need it to launch from a command/cmd startup environment:

powershell -f c:\folder\create-something.ps1 -var1 "my test" -var2 "testing" -var3 @(import-csv c:\some folder\file.csv) -endDate 6/1/2015

How can I get this working?
0
sirbounty
Asked:
sirbounty
  • 3
  • 2
1 Solution
 
oBdACommented:
That won't work with -File, because you want to import in the command line itself. Any specific reason you're not passing the path and then importing in the script?
Anyway, the command line would have to look like that (note the use of double and single quotes):
powershell.exe -Command "& 'c:\folder\create-something.ps1' -var1 'my test' -var2 'testing' -var3 @(Import-Csv 'c:\some folder\file.csv') -endDate 6/1/2015"

Open in new window

If you're restricted to double quotes, the quotes inside the command would need to be escaped with backslashes.
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
oBdA, are you sure about the backslashes? This depends on the calling program; cmd.exe requires a caret ^, but doubling the quotes should also work.
0
 
oBdACommented:
Tested it on a command prompt.
Learned the hard way that the backslash escapes the quote when wondering for quite some time why a simple
robocopy "C:\Source\" "D:\Target\"
threw errors about the arguments ...
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Robocopy is different, this is not the default behaviour. But yes, RoboCopy uses backslash for escaping.
0
 
oBdACommented:
That's not robocopy specific. xcopy.exe and echoargs.exe (part of PowerShell Community Extensions, https://pscx.codeplex.com/releases) exhibit the same behavior, for example.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Protect Your Employees from Wi-Fi Threats

As Wi-Fi growth and popularity continues to climb, not everyone understands the risks that come with connecting to public Wi-Fi or even offering Wi-Fi to employees, visitors and guests. Download the resource kit to make sure your safe wherever business takes you!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now