7

how do I execue a .bat from a powershell script.

I want to do something like:

foreach($item in $list){

  param1= $item.Name
  param2= $item.path

  C:\Filesystem_Batches\test.bat param1 param2

}

Thank you

2 Answers 2

13

You can call

cmd.exe /C "test.bat param1 param2"

In powershell V3 there is a new espace string --% which allow to send "weird" parameters to your exes. exemple :

PS> echoargs.exe --% %USERNAME%,this=$something{weird} 

Arg 0 is <jason,this=$something{weird}>
Sign up to request clarification or add additional context in comments.

1 Comment

If the path or filename for the .bat or .cmd file contains one or more spaces, wrap the path in escaped (escape char is the backtick in PowerShell) double quotes, or use a combination of single and double quotes: cmd.exe /C ""test file.bat" param1 param2" cmd.exe /C '"test file.bat" param1 param2'
6

I am new to Powershell but the following works for me. I am able to run the *.bat from my *.ps1 file. And I am using Powershell 5.1.17134.590.

& .\myFile.bat parm1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.