EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials PowerShell Tutorial PowerShell Batch File
Secondary Sidebar
PowerShell Tutorial
  • Functions
    • PowerShell Functions
    • PowerShell String Functions
    • powershell nslookup
    • PowerShell here string
    • PowerShell Wildcards
    • Regex in PowerShell
    • PowerShell not like
    • PowerShell Filter
    • PowerShell Sleep
    • PowerShell where
    • PowerShell join string
    • PowerShell Exit
    • PowerShell null
    • PowerShell Dictionary
    • PowerShell Location
    • PowerShell Start-Service
    • PowerShell is not digitally signed
    • PowerShell Uptime
    • PowerShell Create Directory
    • PowerShell Trim
    • PowerShell Join-Path
    • PowerShell Execution Policy
    • PowerShell SubString
    • PowerShell Format Table
    • PowerShell Import Module
    • PowerShell ForEach Object
    • PowerShell Alias
    • PowerShell Scheduled Task
    • PowerShell Convert String to Date
    • PowerShell Split String
    • PowerShell Multiline String
    • PowerShell MultiLine Comment
    • PowerShell Rename Folder
    • PowerShell Delete Folder
    • PowerShell String Replace
    • PowerShell join
    • PowerShell xcopy
    • PowerShell Base64
    • PowerShell Tail
    • PowerShell User List
    • PowerShell remove User from group
    • PowerShell JSON Format
    • PowerShell Send Mail
    • PowerShell Convert to String
    • PowerShell Start-Process
    • PowerShell change directory
    • PowerShell Open File
    • PowerShell Batch File
    • PowerShell ZIP
    • PowerShell unzip
    • PowerShell XML
    • PowerShell XML Parsing
    • Remote PowerShell
    • PowerShell Escape Character
    • PowerShell scriptblock
    • PowerShell Executable Location
    • PowerShell Import-CSV?
    • PowerShell Export CSV
  • Basics
    • PowerShell Restart Service
    • PowerShell comment
    • PowerShell Map Network Drive
    • PowerShell Append to File
    • PowerShell print
    • What is PowerShell
    • Uses Of Powershell
    • PowerShell Empire
    • PowerShell Parameter
    • PowerShell Stop Service
    • PowerShell Versions
    • How To Install PowerShell
    • PowerShell uninstall module
    • How to Use PowerShell?
    • PowerShell Logging
    • PowerShell Tools
    • PowerShell Commands
    • PowerShell Version Command
    • PowerShell Administrator
    • PowerShell Modules
    • PowerShell Registry
    • PowerShell block Comment
    • PowerShell Verbs
    • PowerShell list
    • PowerShell add user to group
    • PowerShell Write to Console
    • Variable in PowerShell
    • PowerShell New Line
    • PowerShell prompt for input
    • PowerShell File Extension
    • Powershell Remotesigned
    • PowerShell Write to File
    • PowerShell Ping
    • PowerShell wget
    • PowerShell Global variable
    • PowerShell Get-ADGroup
    • Array in PowerShell
    • PowerShell Multidimensional Array
    • PowerShell Array of Strings
    • PowerShell? join array
    • Useful PowerShell Scripts
    • String in PowerShell
    • PowerShell Switch Statement
    • PowerShell Function Parameters
    • PowerShell vs PowerShell ISE
    • PowerShell test-connection
    • PowerShell Test-NetConnection
    • PowerShell GUI
    • PowerShell Variable in String
    • PowerShell Active Directory
  • Variables
    • PowerShell Variables
    • PowerShell Environment Variables
    • PowerShell set environment variable
    • Hashtable in PowerShell
    • Set Variable in PowerShell
  • Operators
    • PowerShell Operators
    • Comparison Operators in PowerShell
    • Logical Operators in PowerShell
    • PowerShell Boolean
    • PowerShell Like Operator
  • cmdlet
    • PowerShell Wait
    • PowerShell Match
    • cmdlets in PowerShell
    • Start PowerShell from cmd
    • Add-Content in PowerShell
    • Get Help in PowerShell
    • PowerShell Copy-Item
    • PowerShell Remove-Item
    • PowerShell Move-Item
    • Get Command in PowerShell
    • PowerShell Run Command
    • Windows PowerShell ISE
    • Windows Powershell Commands
    • WinRM PowerShell
    • PowerShell Date
    • Powershell Write-Host
    • PowerShell Get-ChildItem
    • PowerShell Sort-Object
    • PowerShell Where Object
    • PowerShell Set-Content
    • PowerShell Set-Location
    • PowerShell Invoke-Command
    • PowerShell Invoke-Webrequest
    • PowerShell Get-Location
    • PowerShell Get-Date
    • PowerShell Get-Service
    • PowerShell Test-Path
    • Powershell Module Path
    • PowerShell Out-File
    • PowerShell if File Exists
    • Powershell Copy File
    • PowerShell Delete File
    • PowerShell New-Item
    • PowerShell Rename-Item
    • PowerShell ComputerName
    • PowerShell Get-Content
    • PowerShell Get-Item
    • PowerShell Get-ADUser
    • PowerShell Grep
    • PowerShell Concatenate String
    • PowerShell Get-Process
    • PowerShell Count
    • PowerShell pause
  • Control Statements
    • If Statement in PowerShell
    • If Else in PowerShell
    • Else If in PowerShell
    • Loops in PowerShell
    • For loop in PowerShell
    • PowerShell While Loop
    • PowerShell do while
    • PowerShell Loop through Array
    • PowerShell add to array
    • PowerShell ForEach Loop
    • PowerShell Break
    • PowerShell Continue
    • Switch Case in PowerShell
    • PowerShell If-Not
    • Try-catch in PowerShell
  • Interview Questions
    • PowerShell Interview Questions

Related Courses

Shell Scripting Course

All in One Data Science Courses

Data Visualization Courses

PowerShell Batch File

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.

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"

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

To call the batch file from the PowerShell script,

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (86,241 ratings)

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
Popular Course in this category
PowerShell Training (2 Courses, 1 Project)
  2 Online Courses |  1 Hands-on Project |   4+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Shell Scripting Training (4 Courses, 1 Project)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
Data Visualization Training (15 Courses, 5+ Projects)4.7
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

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

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

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

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more