You know that feeling when you have your PowerShell script written, saved and now you need to execute it? But every time you want to use the script, you must open the text editor and run it the hard way. The repetitive cycle for each script execution can be quite time-consuming, don’t you think so. If this situation sounds familiar, I have a little trick for you to solve this problem.
Using the command line for easy execution
How can you quickly run your PowerShell scripts without going through complicated steps each time? All you need is to create a simple command that allows you to run your PowerShell script with a double-click. Here’s how:
Step 1: Creating a Run File
The first step is to create a run file that will contain your PowerShell script. We’ll use a text file with the .bat or .cmd extension. Just open any text editor, for example, Visual Studio Code in my example, and insert the following command:
Breakdown of the code:
“powershell.exe” launches Windows PowerShell
“-ExecutionPolicy Bypass” enables unrestricted script running. (Not necessary)
“-File “PathToYourScript.ps1” specifies the script’s path.
“pause” temporarily pauses execution for result review.
Step 2: Save the File
Here, you need to adjust the part “PathToYourScript.ps1” to point to the actual path of your PowerShell script. You might notice the “pause” command at the end, which keeps the window open until you click on it again. After saving the file, for instance, as RunScript.cmd, you can then double-click this file to execute your PowerShell script.
Step 3: Enjoy the Simplicity
When you run the script, a command prompt window will appear, showing you the progress of the script. Thanks to the “pause” command, you can easily check the output and ensure that the script ran correctly.
It’s important to note that the approach outlined here is specifically designed for straightforward manual execution, without reliance on external tools such as triggers or DevOps procedures. By simply double-clicking, you can effortlessly initiate your PowerShell scripts, effectively reclaiming your precious time. The days of grappling with intricate processes are over; this technique promises to streamline your daily activities, freeing up room for more crucial tasks.
I work as a data engineer at Joyful Craftsmen, where my true passion lies in the realm of data. Joyful Craftsmen allows me to continuously develop my skills, leading to steady growth. I particularly enjoy working with tools like Power BI and have recently become interested in PowerShell.
DANG THE TRUNG