. Advertisement .
..3..
. Advertisement .
..4..
“How can I run Powershell script from batch file“? Our team has received this same question from many readers. Today, we will provide simple steps to handle this issue.
How to Run Powershell Script from Batch File
There is no need to rewrite your batch files for every single script (or whenever you have to move the script around). Instead, you may use self-referencing variables to set up the file paths for your PowerShell scripts.
So how can you make it work? It is important to place that batch file in the folder where you put the PowerShell script. The file names should also be similar.
Suppose the PowerShell script is named “MyScript.ps1”. Then your batch file should also be named “MyScript.bat”. Make sure these two files lie in one folder. After that, put those lines in your batch script:
@ECHO OFF
PowerShell.exe -Command "& '%~dpn0.ps1'"
PAUSE
Had it not been for other safety restrictions, that would be everything you need to do to run PowerShell scripts! However, the last and first lines are just matters of preference; the second line is the one that does all the work.
How does that happen? Let us break down the syntax above.
@ECHO OFF switches off the command echoing, which only keeps other commands from popping up on your screen during the batch file’s operation. That line is hidden by your usage of the (@) symbol at the front.
Meanwhile, PowerShell.exe operates the actual PowerShell script. You may call it from CMD windows or batch files to establish PowerShell to bare console-like systems.
It is also possible to run commands directly from your batch files by incorporating the “-Command” parameter and proper arguments. To target the PS1 file, it goes in conjunction with special l %~dpn0 variables.
Operated from the batch file, this %~dpn0 factor assesses each drive letter, folder route, and file names (with no extension) of these batch files.
As PowerShell script and batch files are in similar folders with similar titles, %~dpn0.ps1 will transform into a full file path of your PowerShell script.
PAUSE only pauses your batch execution to wait for some user input. This strategy is handy for the end of the batch files. Why? It means you will have some chances to evaluate command outputs before your window vanishes. The more steps we go through, the more obvious this usefulness becomes.
Let’s say your batch file is properly set up. To help you understand the example, we will save the file as “D:\Script Lab\MyScript.bat” with “MyScript.ps1” lying in that similar folder.
So what will happen when you double-click MyScriopt.bat? Of course, the script did not run yet, which is expected. Let’s do a few more steps to tackle this dilemma once and for all:
Step 1: Add these lines to your scripts.
Write-Output 'Custom PowerShell profile in effect!'
Step 2: Issue this command to flag “MyScript.ps1” as an external-source file.
Add-Content -Path 'D:\Script Lab\MyScript.ps1' -Value "[ZoneTransfer]`nZoneId=3" -Stream 'Zone.Identifier'
Step 3: Modify the script’s second line to insert another parameter to your “PowerShell.exe” command.
PowerShell.exe -ExecutionPolicy Bypass -Command "& '%~dpn0.ps1'"
Step 4: Get Administrator access by changing your second line to:
PowerShell.exe -Command "& {Start-Process PowerShell.exe -ArgumentList '-ExecutionPolicy Bypass -File ""%~dpn0.ps1""' -Verb RunAs}"
Step 5: Issue another change to the second line one last time:
PowerShell.exe -NoProfile -Command "& {Start-Process PowerShell.exe -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""%~dpn0.ps1""' -Verb RunAs}"
The outputs should look like this:
@ECHO OFF
PowerShell.exe -NoProfile -ExecutionPolicy Bypass -Command "& '%~dpn0.ps1'"
PAUSE
or:
@ECHO OFF
PowerShell.exe -NoProfile -Command "& {Start-Process PowerShell.exe -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""%~dpn0.ps1""' -Verb RunAs}"
PAUSE
Conclusion
This article has instructed you to run powershell script from batch file. For other PowerShell issues (such as the null-valued expression glitches), feel free to check ITtutoria Q&A sections! You can also contact us for more help.
Leave a comment