To Run BAT File from PowerShell Script, you can run it manually from the PowerShell (or make the PowerShell execution in the windows scheduler run at a certain time).ย
A batch file is a series of commands or a script in the Windows Operating System that executes a series of tasks on the local or remote machines and it has a. Bat extension. Although PowerShell and Batch are different languages, both can be integrated into each other and helps to call and execute each other.
This article will illustrate different ways to run a Batch file from a PowerShell script.
Step #1 Create a .bat file with commands
For time being, I only used the echo command in the bat file, you can write your logic command as required. For this example, I save the file as CallMe.bat.
echo Write something, it will be used in the command “echo”
pause
Step #2 Create a Powershell script file & call the .bat file
There are many approaches we can call the .bat file from PowerShell, which we can choose based on our purpose. This step will guide you on how to Add the Batch file to a PowerShell script to run automatically when the PowerShell script is being run.
Method: 1
One way of running a Batch file from the PowerShell script is using the Start-Process cmdlet.ย The Start-Process cmdlet allows you to run one or multiple processes on your computer from within PowerShell.
Itโs designed to run a process asynchronously or to run an application/script elevated (with administrative privileges).
To run the Batch file, add the following line of code to the PowerShell script:
–Filepath specifies the path of the Batch file.
–NoNewWindow starts the process in the current window (add this at end of the script to mention not to open the cmd window).
Start-Process -FilePath ‘C:\blog\callme.bat’
To run the Batch file as administrator, add -verb run as in the above code. This command is useful when your .bat file contains commands which require administrator privileges to run on execution.
Start-Process -FilePath ‘C:\blog\callme.bat’ -Verbose Runas -NoNewWindow
Output
Method: 2
/c referred to Carries out My Command and then terminates
cmd.exe /c ‘C:\blog\callme.bat’
Method : 3
& C:\blog\callme.bat
Leave A Comment