Above all, You can use this how to run PowerShell script tutorial for all kind of scripts. Further, you can execute a PowerShell script (*.ps1 file) in the PowerShell console to run the commands sequentially. Before starting, you may need to consider following things.
- How to create PowerShell Script – .ps1 File
- Execution of Scripts is Disabled on this System
How To Run PowerShell Script (Quick)
- Firstly, Right click the PowerShell script in File Explorer.
- Secondly, Select Run with PowerShell from the context menu.
The script will run with an execution policy of Bypass. The execution policy for the computer or user isn’t changed.
Prerequisites to Run Scripts
- You need to run PowerShell as an Administrator.
- You need to set your PowerShell execution policy to a permissive value or be able to bypass it.
How To Run PowerShell Script
In fact, This all depends on PowerShell script writers and administrators who manage their scripts differently. So, I am writing based on my experience and with sequences that will help you.
1. PowerShell Execution Policy Bypass
In fact, we can use PowerShell execution policy bypass to run the script without getting Running Scripts is disabled on this system error. Here is the complete command, you may update according to your script name and location.
PowerShell.exe -File D:\Workspace\PowerShell\Process.ps1 -ExecutionPolicy Bypass
2. Administrator Windows PowerShell
Further, you need to set execution policy to allow running signed scripts on your computer. Then, you can run PowerShell as an Administrator to execute the scripts.
- To open the Administrator Windows PowerShell, click on the Start button (or search button), type PowerShell, and click Run as Administrator (as shown in image).
3. Run With PowerShell In Context Menu
Finally, you can run PowerShell (.ps1) file with Run with PowerShell option in context menu. This methods works great but required to setup proper execution policy and sometimes pause in the end of PowerShell code.
PowerShell Closes Immediately
Additionally, you may notice that PowerShell closes immediately while you Right click the PowerShell script in File Explorer. There can be few possible reason for this.
- Firstly, You need to run PowerShell as an Administrator.
- Secondly, You can confirm Running Scripts is Disabled on this System
- Thirdly, add
Pause
in the end of your code and this will stop the screen as below.
- Fourth, open PowerShell Console, navigate to PowerShell scrip location and run the command like
- Switch to directory residing PowerShell Script. For example, PS D:\Workspace\PowerShell> in my scenario.
- Type
.\Process.ps1
to run PowerShell script and this will not exit the PowerShell.
- Fifth, Type -noexit switch when you run the script in PowerShell or CMD using command instead of switching PowerShell script directory.
PowerShell -noexit D:\Workspace\PowerShell\Process.ps1
References
You can read following references for better understanding.