What is a .bat file?

A batch file is a text file that the Windows cmd.exe command line processor executes as a batch job. It contains a series of line commands in plain text that are executed to perform various tasks, such as starting programs or running maintenance utilities within Windows.

You can also read  – How to create Basic Chart by reading excel using PowerShell and How to remove the Multiple bindings (URLs) from IIS using PowerShell script

Why call my PowerShell script from a batch file

You can’t double-click to run .PS1 files, but you can execute a .BAT file that way. So, we’ll write a batch file to call the PowerShell script from the command line. There are several good reasons for doing this:

  • Non-technical Users are often tackled with PowerShell.
  • When a user double-clicks on a PowerShell script file (*.ps1) in default it will open the file in the editor rather than executing it.
  • Many scripts may require admin privileges in order to run correctly and in this case, the user need-aware of how to run a PowerShell script as admin without going into a PowerShell console and it will be difficult to run this for Non-technical users.

So in this post, we are going to discuss how you can call the PowerShell script from a batch file.

STEP #1: Creating sample .ps1 file

Writing simple site validation PowerShell script and save as SiteValidationTestThroughBATFile.ps1

STEP #2: Creating a batch file with .bat extension

I had created the simple notepad file and saved it with the .bat extension as shown below. The below .bat file has created file name with ClickToValidationSite_EXE.bat

STEP #3: Calling .ps file inside the .bat file

Open the ClickToValidationSite_EXE.bat by right with the Edit option, it will open in the notepad. Here we going to call the PowerShell script which will validate and provide the status of the site.

In the first line, the @echo off commands are used to disable the echoing or prevents the echo on the screen.
PowerShell.exe called from any CMD window or batch file to launch PowerShell. -ExecutionPolicy Unrestricted
-Commend will bypass the execution policy so the script will run without any restriction.

OUTPUT