Danijel-James W
Danijel-James W

Reputation: 1506

Send commands into a Start-Process function call after the initial commands

I have the following commands that I need to run in a Command Prompt from PowerShell, in order.

  1. /k "C:\Program Files (x86)\Windows Kits\10\Assessment and Deployment Kit\Deployment Tools\DandISetEnv.bat"
  2. copype amd64 C:\WinPE_amd64_PS

I can execute the first command by the following PowerShell command:

Start-Process -FilePath 'C:\Windows\system32\cmd.exe' -ArgumentList '/k "C:\Program Files (x86)\Windows Kits\10\Assessment and Deployment Kit\Deployment Tools\DandISetEnv.bat"' 

That works fine, but I can't pass the second command into the Start-Process. How do I go about doing that? The DandISetEnv.bat file needs to be loaded and the Command Prompt remain open using the /k switch.

Is there a switch or command I can put between the first and second command, or redirect the next command into the Command Prompt process that was started up?

Upvotes: 1

Views: 326

Answers (1)

mklement0
mklement0

Reputation: 439852

Since your intent is to pass multiple commands to cmd, you need to separate these commands with its statement-chaining operator, &:

# Define the array of commands to pass to cmd.exe
$cmdCommands = 
  '"C:\Program Files (x86)\Windows Kits\10\Assessment and Deployment Kit\Deployment Tools\DandISetEnv.bat"',
  'copype amd64 C:\WinPE_amd64_PS'

Start-Process -FilePath cmd -ArgumentList ('/k ' + ($cmdCommands -join ' & '))

Note that if you wanted to only execute the latter command if the former succeeds, you should use && instead of &.

Upvotes: 1

Related Questions