Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call PowerShell Script From Batch File With All Parameters

This question and this blog post address how to pass particular parameters into a PowerShell script from a batch file.

How can one pass all parameters into the PowerShell script? Basically I want to splat all the parameters so that the batch file passes all arguments through transparently.

Edit for more context:

I'm currently using a line like:

PowerShell.exe -Command "& '%~dpn0.ps1' '%1' '%2'"

This works, but creates a redundancy between the files such that, if I update the PowerShell script to take different arguments, I have to update the batch script as well. What would be nice is if I could do something like:

PowerShell.exe -Command "& '%~dpn0.ps1' '%*'"
like image 208
jtpereyda Avatar asked Sep 11 '17 20:09

jtpereyda


People also ask

How do you call a batch file with parameters from a PowerShell script?

To run a batch file ( cmd.exe shell script) from a PowerShell prompt, just type the batch file's name, followed by its parameters, and press Enter . Remember: PowerShell is a shell, which means it runs command you type, just like cmd.exe does. For example: D:\load.

How do I run a PowerShell script with a parameter?

You can run scripts with parameters in any context by simply specifying them while running the PowerShell executable like powershell.exe -Parameter 'Foo' -Parameter2 'Bar' . Once you open cmd.exe, you can execute a PowerShell script like below.

Can we pass parameters to batch file?

Batch parameters (Command line parameters): In the batch script, you can get the value of any argument using a % followed by its numerical position on the command line. The first item passed is always %1 the second item is always %2 and so on. If you require all arguments, then you can simply use %* in a batch script.

How do I run a PowerShell script from the command line?

Running a PowerShell script from the Command Prompt If you would like to run a PowerShell script in CMD, you'll need to execute it by calling the PowerShell process with the -File parameter, as shown below: PowerShell -File C:\TEMP\MyNotepadScript. ps1. PowerShell -File C:\TEMP\MyNotepadScript.


1 Answers

Try the following:

powershell -File "%~dpn0.ps1" %*
  • In batch files, %* represents all arguments passed.[1]

  • -File is the parameter to use to invoke scripts via PowerShell's CLI.

  • All remaining arguments are then passed through as as-is (whereas -Command would subject them to another round of interpretation by PowerShell[2] ).

[1] Note that for cmd.exe (batch files) to recognize an argument with embedded whitespace as a single argument, you must enclose it in "...".
For instance, if you wanted to pass arguments a and b c to batch file file.cmd, you'd have to call it as
file a "b c".
To pass embedded " instances, \-escape them; e.g., "\"b c\"" makes PowerShell see "b c", including the double quotes.
If you respect these rules, %* - without quoting - properly passes the array of arguments through.
Do not use "%*" - it won't work as expected.

[2] In effect, -Command causes all following arguments to be joined by a single space each, and the resulting string is then interpreted as a PowerShell command - that is, after the arguments are parsed by the rules of cmd.exe (batch files), they are subject to another round of parsing, by PowerShell.
Unfortunately, PowerShell has always worked this way, but the behavior is obscure, and is likely to cause even more confusion in the Unix world, now that PowerShell has gone cross-platform - see this GitHub issue.

like image 198
mklement0 Avatar answered Sep 19 '22 01:09

mklement0