How can I start a powershell script with parameters from a batch file [duplicate]

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 the CMD 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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật