piotrp
piotrp

Reputation: 324

Execute PowerShell command from cmd - quotes issue

I have to execute two commands (including a PowerShell one) in new console window. Let me explain:

I am able to execute a PowerShell command from cmd in a new console window (I can specify the command inside quotes):

start /W cmd /K powershell -command "&{Write-Host 'Hello World!';}"

And I can execute two commands (I can write both commands in quotes):

start /W cmd /K "echo Hello && echo World"

But how can I do it together (execute two commands, one of them PowerShell)? I would like to do it without any .bat or .ps script.

I tried something like following (and some variations with escaping character), but I cannot deal with this quotes issue (there are quotes for joining two commands and inside quotes for PowerShell command).

start /W cmd /K "echo Hello && powershell -command "&{Write-Host 'Hello World!';}""

Upvotes: 2

Views: 514

Answers (1)

AdamL
AdamL

Reputation: 13141

You need to escape & in powershell's ScriptBlock for cmd:

start /W cmd /K "echo Hello && powershell -command "^&{Write-Host 'Hello World!';}""

Upvotes: 2

Related Questions