How to Execute a .bat File within a PowerShell Job

Most of the time, you run Windows batch files using the Command Execution Method, which replicates running them in a command prompt window (cmd.exe).  But, what if you need to perform additional processing within the same job and you are running that batch processing in PowerShell? You have several methods to launch Windows batch files from within PowerShell using these methods:

You can start a command procedure from PowerShell with the following code. Replace the path and file with your own information.

C:Pathfile.bat

Once you’ve called your batch file, you can customize it to the task at hand. For example…

If you want to capture the output of the .bat file, you can use:

$out = C:Pathfile.bat

If you want to start a process with your .bat file, you can use the PowerShell start-process cmdlet:

start-process C:Pathfile.bat

And, if you if you want to control cmd.exe, you can use this:

start-process "cmd.exe" "/c C:Pathfile.bat"

The start-process cmdlet is a standard PowerShell cmdlet, so anyone can use it. JAMS users leverage it regularly and combine it with JAMS specific cmdlets in our PowerShell Scheduler to add intelligent automation to batch files.

Explore PowerShell Scheduling