Share via


Call a batch file with parameters passed to it

Question

Saturday, May 19, 2018 11:29 PM

I am trying to call a batch file in ps1 file and pass 2 parameters to it. something like below with no luck please advise. Thanks

Invoke-Item C:\batchfile.bat $True $False

All replies (7)

Sunday, May 20, 2018 4:10 AM

You cannot pass Booleans to a shell script.  CMD shell does not support Boolean, obects or arrays.  It can only receive strings.

c:\batchfile.bat $true $false

batchfile.bat  =
@echo %1
@echo %2

\(ツ)_/


Sunday, May 20, 2018 1:14 PM

I need to use Invoke-Item to call batchfile.bat but it only works without parameters. i.e.

PS C:\ Invoke-Item C:\batchfile.bat

ok but got error below when add string parameters:

PS C:\ Invoke-Item C:\batchfile.bat abc 123
Invoke-Item : A positional parameter cannot be found that accepts argument 'abc'.
At line:1 char:1

  • Invoke-Item C:\temp\echo.bat abc 123

    + CategoryInfo          : InvalidArgument: (:) [Invoke-Item], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.InvokeItemCommand


Sunday, May 20, 2018 1:18 PM

You do NOT need Invoke-Item.  Invoke-Item is not used for batch files.  Just execute the batch file as I posted above.  It works just like I posted.  Look at the image.

\(ツ)_/


Sunday, May 20, 2018 1:36 PM

To invoke a external command you need to use invoke-expression.

For example

$result = invoke-expression "c.bat param1 param2"


Sunday, May 20, 2018 1:51 PM

There is no need to use invoke when running a batch file or script.  PowerShell is designed to run those without any help.  Invoke-Item is used to open a document in its program and Invoke-Expression is used when you have a need to evaluate a complex text expression.

\(ツ)_/


Sunday, May 20, 2018 3:12 PM

Thank you all for reply. I have a special need for this: as soon as launching batchfile.bat, I need to continue execute next scripts without wait. It seems only Invoke-Item can do that. Others have to wait to finish the batch file execution before continue, like Invoke-Expression, Invoke-Command or direct run the batch.. Thanks. 


Sunday, May 20, 2018 4:02 PM

You cannot pass parameters with Invoke-Item.

Use Start-Process for that.

** Start-Process c:\batchfile.bat -Args $true,$false**

\(ツ)_/