How to Run BAT File From Powershell Script
- Create and Run a PowerShell Script
- Run a Batch File From PowerShell Script
- Run a Batch file From PowerShell Script With Arguments
- Run Batch Commands From PowerShell
- Conclusion
A Batch file consists of a series of commands that execute line by line in a sequential manner. These Batch files are executed in cmd.exe, a command-line interpreter initially released for the Windows NT family.
Similarly, a PowerShell file is a text file consisting of a list of commands executed by the PowerShell command-line interface; it is saved with the .ps1 extension. It is the latest shell and scripting tool for Windows and a more advanced cmd version.
To run a .bat file from a PowerShell script, you can run it manually from the PowerShell. But, adding a .bat file to a PowerShell script to run it automatically without any user intervention may not sometimes run due to some minor mistakes that should be taken care of.
This article will illustrate different ways to run a Batch file from a PowerShell script.
Create and Run a PowerShell Script
To create a PowerShell script, open Notepad and add the commands to be executed. Save the file with the .ps1 extension to save it as a PowerShell file.
You can use any text editor to create a file.

To run a PowerShell script, right-click on the PowerShell file and click on Run with PowerShell. You can also edit the file in PowerShell ISE.

Run a Batch File From PowerShell Script
To run a .bat file from the PowerShell script, add the following line to the PowerShell script:
& .\testfile1.bat

But, this only works when testfolder is the relative path or when using the drive letter in the path. A more stable approach can be made.
For example, we have a .bat file named testfile.bat, which consists of commands which send the output to a text file named opt.txt. Add the Batch file to a PowerShell script to run automatically when the PowerShell script is being run.
Add the following line to the PowerShell script for running the Batch file:
cmd.exe /c 'testfile1.bat'

Output:

Another way of running a Batch file from the PowerShell script is using the Start-Process cmdlet. To run the Batch file, add the following line of code to the PowerShell script:
Start-Process -FilePath 'C:\Users\Aastha Gas Harda\Desktop\testfile1.bat' -NoNewWindow

Where:
-Filepathspecifies the path of the Batch file.-NoNewWindowstarts the process in the current window.
To run the Batch file as administrator, add -verb runas in the above code:
Start-Process -FilePath 'C:\Users\Aastha Gas Harda\Desktop\testfile1.bat' -Verbose Runas -NoNewWindow

This command is useful when your .bat file contains commands which require administrator privileges to run on execution.
Run a Batch file From PowerShell Script With Arguments
To run a Batch file from the PowerShell script that contains arguments, you can do so by using the -ArgumentList parameter and specifying each argument in an array of strings. By taking the above example, add the following line of code in the PowerShell script:
Start-Process -FilePath "cmd.exe" -ArgumentList @("/B", "/C", "`"", "^`"testfile1.bat^`"", "^`"$inputFilePath^`"", "`"") -NoNewWindow;

Run Batch Commands From PowerShell
Rather than the Batch file, you can also execute Batch commands directly from the PowerShell script. Add the following line of code to execute the echo command to print the output as hello world.
Start-Process "cmd.exe" '/c echo helloworld' -NoNewWindow

Conclusion
So, we discussed how to run a Batch file from the PowerShell script in the safest way possible. We also discussed how to run Batch commands directly from the PowerShell instead of a PowerShell script without user intervention.