EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials PowerShell Tutorial PowerShell Batch File
 

PowerShell Batch File

Updated March 6, 2023

PowerShell Batch File

 

 

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.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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.

powershell batch file 1

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:

powershell batch file 2

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:

powershell batch file 3

In the Same console with -NoNewWindow Parameter.

Start-Process -FilePath "C:\Temp\TestBatch.bat" -NoNewWindow

Output:

powershell batch file 4

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:

example 1-1

To call PowerShell core version (6.0 or above), type Pwsh.exe.

pwsh.exe

Output:

pwsh.exe

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:

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:

powershell 2

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 1

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,

execute 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:

example 2

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 –

  1. PowerShell Like Operator
  2. PowerShell where
  3. PowerShell uninstall module
  4. PowerShell Match

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW