When working with PowerShell scripts, it’s common to encounter scenarios where you want to launch an automated operation on demand by running the script file and then viewing its output at the end of the process.
On a modern Windows system, you can right-click on a PowerShell file and select the Run with PowerShell option from the context menu. While this provides the means to run a script on demand, unfortunately, the terminal window will close once the script has finished executing.
To work around this issue, we need to utilise a batch/command file in conjunction with the pause
command.
In this post, I will briefly demonstrate how to implement the aforementioned solution.
Command file
Assume we have a PowerShell file named ‘ImportantFilesBackup.ps1’ and we want the ability to execute this file on demand and view its output within the terminal/command window host before closing.
We can create a Command file named ‘RunImportantFilesBackup.cmd’ with the following contents.
@echo off powershell -NoProfile -ExecutionPolicy Bypass -File C:\Scripts\ImportantFilesBackup.ps1 pause
Let’s break down what this simple script is doing.
Echo
The @echo off
command is used to suppress command echoing, making the output cleaner.
Without this, we would see the following output displayed within the host window.
C:\Scripts>powershell -NoProfile -ExecutionPolicy Bypass -File C:\Scripts\ImportantFilesBackup.ps1
PowerShell
- The
powershell
command invokes the PowerShell executable from within the command script. - The
-NoProfile
parameter ensures that the script runs in a clean environment, without loading any custom PowerShell profiles that might interfere with the execution. - Specifying
-ExecutionPolicy Bypass
temporarily overrides the system’s execution policy, ensuring that the script runs even if restrictive policies are in place. This is particularly useful for environments where changing the global execution policy isn’t an option. - The
-File
parameter is used to specify the path to the script file to execute. In this case, a full path is specified, but a relative path is also valid.
Pause
The pause
command halts execution after the PowerShell script has completed, waiting for input from the user before the terminal/command window closes. This provides the opportunity to review any messages that were written to the output during script execution.
Demo
Assuming you have a C:\Scripts directory and have placed the PowerShell script and Command file in the folder, you should be able to double-click on the Command file to test things out.
Below is the output of the PowerShell script within the Command Prompt window.
As per the “Press any key to continue . . .” message, pressing any key will close the window.
With this in place, you now have a convenient way to launch a PowerShell script and review its output and can also close the window easily via a keystroke.
Conclusion
In this post, I provided a quick tip regarding PowerShell scripts and how you can run them on demand while not missing out on the final result. By using a Command file, we were able to pause execution as soon as the PowerShell script completes, to keep the host window open.
It’s important to be careful when using the pause
command in production scripts, but when it is used for this specific purpose where the intention is always to manually launch the script and review the output, it makes a lot of sense.
Comments