I would like to start a powershell script from a batch file and ensure the script closes before the batch file proceeds to the next step. I’ve been unable to figure out how to even launch the script with parameters.
The following command works when run in directly PowerShell:
Start-Process powershell -ArgumentList "-NoProfile -ExecutionPolicy Bypass -File `"C:my file pathmyscript.ps1`" -fruit banana -car toyota"
However I need to be able to run this from a batch file (CMD). The below does not work.
Seems to be an issue with quoting but unfortunately single quotes are ignored.
powershell -Command "Start-Process powershell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File `"C:my file pathmyscript.ps1`" -fruit banana -car toyota' -Wait"
Why doesn’t this work and why don’t single quotes work?
2
The (quoting) invocation issue here, is actually a quite common and repeating type of question, see e.g.:
- Escaping quotes in powershell.exe via command prompt
- How do I escape “Quotes” in a powershell string?
- Escape double quotes in powershell -Command
Meaning that I could just answer this specific command line that does work instead but I rather give you some general guidance:
In the invocation examples in this answer, I use the following C:my file pathmyscript.ps1
:
Param($Fruit, $Car)
Write-Host 'Fruit:' $Fruit
Write-Host 'Car:' $Car
$Null = Read-Host 'Press <enter> to continue'
Keep it simple 😎
Often, in situations like this, there are more shells and processes embedded than necessarily. This makes the command expensive and increases the complexity of the command line. As in your example, there is no reason to use the Start-Process
cmdlet to “ensure the script closes before the batch file proceeds to the next step“. PowerShell’s dot-sourcing feature allows you to invoke your script with an easier and less expensive command:
. 'C:my file pathmyscript.ps1' -fruit banana -car toyota
Dot-sourced commands also run synchronously. A difference with Start-Process
though, is that they run in the same scope of the caller (see: about scopes) but that shouldn’t be issue when invoking a single script from a CLI (command line interface) as PowerShell.exe
or CMD.exe
.
This means if you want invoke your script from PowerShell.exe
, you could do:
PowerShell -NoProfile -Command { . 'C:my file pathmyscript.ps1' -fruit banana -car toyota }
Note that the PowerShell.exe
command given from the PowerShell host gives you an extra possibility to enclose a script, which is using curly brackets ({ ... }
) rather than embedding your command (that already migth contain quotes) in a new string. In other words, within the ScriptBlock (enclosed by the curly brackets) the standard quoting rules apply.
Besides, if you want invoke your script from PowerShell.exe
command-line interface, you might also directly invoke your script using the -File
parameter:
PowerShell -NoProfile -File 'C:my file pathmyscript.ps1' -fruit banana -car toyota
The double-quote monster 😱
I probably took already away all the fun with the last command line that should also work from the CMD
command line if you replace the single quotes with double quotes:
PowerShell -NoProfile -File "C:my file pathmyscript.ps1" -fruit banana -car toyota
But let’s assume that you would like to use a cmdlet as Start-Process
with a parameter that needs to be quoted as in your example: -File "C:my file pathmyscript.ps1"
. As apposed to PowerShell, the CMD
command line only understands double quotes for argument enclosure, and…
Here comes the crux: PowerShell.exe
eats double quotes!
The double quotes are used to enclose arguments but they are basically gone ones the argument is determined (btw. it doesn’t understand curly brackets when the PowerShell.exe
command is given from the CMD
command line).
To prove this (using the CMD
command line):
PowerShell -Command ". 'C:my file pathmyscript.ps1' -fruit ba"na"na -car to"yo"ta"
Fruit: banana
Car: toyota
Note
When you invoke the same command from the (Windows) PowerShell CLI it is somewhat smarter how it handles quotes, but the difference makes it sometimes even more confusing.
So, the easiest way to get around this, is simply avoid any embedded double quotes and use single quotes ('...'
) or even no quotes where possible for the PowerShell arguments which is less of an issue if you simplified the invocation:
PowerShell -Command ". 'C:my file pathmyscript.ps1' -fruit 'ba na na' -car toyota"
Fruit: ba na na
Car: toyota
Note 1
A PowerShell command given from theCMD
command line still requires to enclose the outer CMD argument with double quotes if it contains any spaces.
Note2
In argument mode, PowerShell doesn’t require any quoting (as in-fruit banana
) unless the argument contains special characters (including spaces).
And in case you really are in need to pass double quotes, you are required to escape them to: ""
, e.g.:
PowerShell -Command ". 'C:my file pathmyscript.ps1' -fruit '""Banana""' -Car '""Toyota""'
Fruit: "Banana"
Car: "Toyota"
For details on this, I recommend you to read the helpful answer from mklement0 about the tricky double quote exceptions and how to robustly escape them when calling (PowerShell) from cmd.exe
: /a/49060341/1701026
3