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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#######################################################################<br> #Project : Creating Powershell Mulitple Reports in HTML with CSS Format<br> #Developer : Thiyagu S (dotnet-helpers.com)<br> #Tools : PowerShell 5.1.15063.1155<br> #E-Mail : mail2thiyaguji@gmail.com<br> ###################################################################### $_URL = 'https://dotnet-helpers.com' $request= [System.Net.WebRequest]::Create($_URL) $response = $request.getResponse() if ($response.StatusCode -eq "200"){ write-host "`nSite - $_URL is up (Return code: $($response.StatusCode) - $([int] $response.StatusCode)) `n" -ForegroundColor green } else { write-host "`n Site - $_URL is down `n" ` -ForegroundColor red } |
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.
1 2 3 4 5 |
@echo off powershell.exe -ExecutionPolicy Unrestricted -Command ". 'C:\PowerShell\SiteValidation.ps1'" TIMEOUT /T 10 |
OUTPUT: Run PowerShell Script from a Batch
Here’s how to automate with AHK:
“GTA5script := "$gtas = Get-Process -ProcessName
"
; foreach ($p in $gtas) {$p.ProcessorAffinity=” mask “; echo $p.ProcessorAffinity}
; Start-Sleep -s 2″WinSetTitle(script)
Run(‘”PowerShell.exe” -command “‘ script ‘”‘)
`
https://github.com/tallpeak/AHK/blob/main/GTA/AFKMoney/afkmoney_v2.ahk#L1639
It is a bit more complex than that: what I would like to get is a dropdown list of preselected names when I click on rename function.
Another question: is it possible to attach a file on this comment section? It would be far easier if I could attach a function diagram to show what I am intending to achieve.
Whether you looking for adding some name (like create, rename) in the explorer when we right-click in windows?
Is it possible to create a drop down menu of names under the rename command in Windows Explorer?