Updated March 6, 2023
Definition of PowerShell Batch File
A batch file is a series of commands or a script in the Windows Operating System or in DOS which can run a single command or multiple commands at a time and executes series of tasks on the local or remote machines and it has a. Bat or. Cmd extension. Although PowerShell and Batch are different languages, both can be integrated into each other and helps to call and execute each other tasks.
Syntax:
To call the PowerShell script from the Batch file we can use the below syntax in the Batch file.
Powershell.exe -ExecutionPolicy Unrestricted -command "PowerShell file path"
To call the batch file from the PowerShell script,
Start-Process -FilePath “BatchFilePath”
To control the command prompt execution,
Start-Process "cmd.exe " "/c Batch File Path"
How does the batch file work in PowerShell?
Batch files are the series of commands that we write in the command prompt or cmd.exe individually. They are written using a different syntax than PowerShell and works on the Windows Operating system.
To simply call the cmd.exe from the PowerShell, we can use the below command.
Start-Process -FilePath cmd.exe
This command will open new command prompt window from the PowerShell console.
If you use -NoNewWindow parameter, PowerShell will start the cmd.exe process inside the PowerShell console only and the PowerShell console will be converted to the cmd console as shown below.
Start-Process -FilePath cmd.exe -NoNewWindow
Output:
Similar way if we have the batch file, we can call it from the PowerShell console.
For example, we have the below BatchFile content and the file name is called, TestBatch.bat and we need to execute the batch file using PowerShell then,
TestBatch.bat file:
@echo off
echo Hello WOrld
pause
To call batch file,
Start-Process -FilePath "C:\Temp\TestBatch.bat"
Output:
In the Same console with -NoNewWindow Parameter.
Start-Process -FilePath "C:\Temp\TestBatch.bat" -NoNewWindow
Output:
Before starting how we can call the PowerShell file from the batch file, we will use how to call PowerShell from the command prompt window.
When the PowerShell is installed on the machine, it creates an entry into the environment variable so you can execute both PowerShell and PowerShell core versions from the cmd.
For example,
To call PowerShell from the cmd just type PowerShell.exe as shown below.
PowerShell.exe
Output:
To call PowerShell core version (6.0 or above), type Pwsh.exe.
pwsh.exe
Output:
This is the way we can invoke the PowerShell process from the cmd if the Powershell versions are installed and set up correctly.
Next step we will execute the PowerShell command from the command prompt.
Powershell.exe -ExecutionPolicy Unrestricted -Command "Write-Output 'Hello from CMD'"
Output:
In the above example, we are using PowerShell.exe to call PowerShell, Setting execution policy to UnRestricted so when we run the command it should not block and third using -Command parameter to write the output.
Now we have the above-mentioned command, we can save this command in a batch file (TestPSBatch.Bat) and execute it from the cmd window and it should work.
TestPSBatch.Bat file:
@echo off
Powershell.exe -ExecutionPolicy Unrestricted -Command "Write-
Output 'Hello from CMD'"
Executing batch file,
C:\Temp\TestPSBatch.bat
Output:
You cal also directly execute that batch file from that location by double-clicking on it.
In the above example, we if use Pwsh instead of PowerShell in a batch file then it should also work.
Examples
Let us discuss examples of PowerShell Batch File.
Example #1: Calling PowerShell script from the Batch file.
This is discussed already, to call a PowerShell script from a Batch file, we can use the below example. Suppose we have a PS script that copies files from source to destination and we need to execute it using the batch file.
This is the TestPS.ps1 content that we need to execute using a batch file.
Copy-Item C:\Temp\envvariables.csv -Destination C:\Temp\Test\ -Verbose
We have created a Batch file copy.bat and its content is as below.
@echo off
PowerShell.exe -ExecutionPolicy Unrestricted -File C:\temp\TestPS.ps1
When we execute Copy.Bat it should show output as below.
Example #2: Calling batch script from the PowerShell script
This example shows how we can call the batch file from the PowerShell.
Testbatch.bat file.
@echo off
echo Hello WOrld
PowerShell script we need to execute to run a batch file.
Start-Process -FilePath C:\Temp\TestBatch.bat -NoNewWindow
Output:
After executing Testps.ps1 file,
To run the batch script as the administrator, use -Verb parameter.
Start-Process -FilePath C:\Temp\TestBatch.bat -Verb Runas
Example #3: Running PowerShell command from the batch file
For example1 we can also use directly batch file content in the cmd to run the PowerShell command. However, it is a single-line command. Let say if we want to run multiple PowerShell commands from the cmd window then it is also possible.
In this example, we are going to Copy the file, restart the service and kill the notepad process using the Batch file which executes the PowerShell command as shown below.
PowerShell.exe -executionpolicy Unrestricted Invoke-command -scriptblock { Copy-Item C:\temp\10vms.csv -Destination C:\Test1 -Verbose ; Stop-Service Spooler -Verbose ; Stop-Process -Name "Notepad" -Force -Verbose }
Output:
To run a command remotely we need to provide the computer name as shown below. It will create a new directory on the C:\CMDDir path on the remote server name TestMachine.
PowerShell.exe -executionpolicy unrestricted Invoke-Command -ComputerName TestMachine -scriptblock {New-Item C:\cmddir}
Example #4: Running Batch commands from the PowerShell
To run the batch commands from PowerShell, we can use the Start-Process cmdlet as earlier explained and which executes a cmd command on the server.
Start-Process "cmd.exe" '/c mkdir C:\cmddir' -NoNewWindow -Verbose
The above command creates directory cmddir on the local server.
Conclusion
A combination of PowerShell and Batch files is very much useful because many times new users are unaware of how to run PowerShell when the batch file can easily be executed. PowerShell batch files are also useful with Third-party software like BladeLogic which doesn’t directly support the PowerShell extension but supports the batch files. In such a case we can run a batch job to execute PowerShell commands remotely.
Recommended Articles
This is a guide to PowerShell Batch File. Here we discuss the Introduction, syntax, How does the batch file work in PowerShell? and examples. You may also have a look at the following articles to learn more –