Safest way to run BAT file from Powershell script

cmcginty picture cmcginty · Dec 17, 2013 · Viewed 241.3k times · Source

I can not get a powershell script to execute a bat file directly. For example, this works on the command line:

.\\my-app\my-fle.bat

When I add this command to a script, it outputs:

The term '.\\my-app\my-file.bat' is not recognized as the 
name of a cmdlet, function, script file, or operable program. 
Check the spelling of the name, or if a path was included, 
verify that the path is correct and try again.

I also tried the following, with the same result:

& .\\my-app\my-fle.bat
& ".\\my-app\my-fle.bat"
\my-app\my-fle.bat
& \my-app\my-fle.bat
& "\my-app\my-fle.bat"

Note: It must return the lastexitcode as I need to verify success of batch.

Answer

cmcginty picture cmcginty · Dec 19, 2013
cmd.exe /c '\my-app\my-file.bat'