How To Run a PowerShell Script? The 2 Best Methods in 2024
- Adjust the Execution Policy to allow script execution by using the "Set-ExecutionPolicy RemoteSigned" command which balances security and functionality.
- Run PowerShell scripts through different methods such as "Run with PowerShell" Windows PowerShell CMD or PowerShell ISE based on your needs.
- Create PowerShell scripts using Notepad or PowerShell ISE to automate tasks and streamline system administration.
From troubleshooting networks to managing servers to providing user support, and optimizing system performance, System administrators have a lot to work with. And what’s better than automating a few of these tasks? That’s where PowerShell scripts enter. Written with just a text editor, PowerShell scripts allow you to automate repetitive tasks and handle administrative queries efficiently.
However, a crucial step in your PowerShell journey is learning how to run these scripts. In this article, we’ll help you get started by explaining everything you need to know for running your first PowerShell script. Along the way, we will also look at how you can create your own PowerShell scripts and a few examples to spark your creativity. So, let’s jump right in!
Table of Contents
Step 1: Setting the Execution Policy
When trying to execute PowerShell scripts for the first time, you are likely to encounter an error stating, “cannot be loaded because running scripts is disabled on this system“. This error occurs because your execution policy is set to Restricted, RemoteSigned, or AllSigned. So the first step to running PowerShell scripts is understanding the purpose of the execution policy and configuring it.
↪ The Execution Policy and its Levels
The PowerShell script Execution Policy is a system-wide configuration setting that controls whether PowerShell scripts can be executed, based on their source and digital signature. By default, this policy is set to “Restricted,” blocking all script execution. This helps minimize risks associated with running scripts downloaded from the internet, ensuring controlled access and security on your system.
There are four levels for the Execution Policy:
- Restricted: This policy is set by default on all Windows client computers and restricts the execution of all scripts.
- Unrestricted: For any non-Windows devices this policy is set by default and allows you to run any script. However, it still returns a warning message before executing a script that’s downloaded from the Internet.
- Remote Signed: This policy allows you to either run any script that is digitally signed or written on your local computer even if it doesn’t have a signature.
- All Signed: Whether the script is downloaded from the Internet or written locally, all of them must be signed digitally by a trusted publisher to be executed under this policy.
READ MORE: How to Fix PowerShell Not Opening on Windows? ➜
↪ Changing the PowerShell Execution Policy
Now that we know the significance of the execution policy and the different ways it works in, let’s look at adjusting these restrictions. Here’s a detailed rundown on how to change the PowerShell Execution Policy:
- Open up Windows PowerShell using the Windows Search bar and make sure to run it as an Administrator.
- Next, run the “Set-ExecutionPolicy RemoteSigned” PowerShell command to change your system’s execution policy. We will be changing the execution policy to Remote Signed as it offers a balanced level of security. However, you can choose any one of the four based on your requirements.
- As and when you enter that, you should see an output asking you to confirm the action. Here, Enter Y for Yes and press the Enter key to confirm the policy change.
- To verify your current execution policy, you can use the “Get-ExecutionPolicy” command.
Step 2: Run PowerShell Scripts
Now that you have configured the execution policy, you are all ready to run your first PowerShell script. There are several options available for running PowerShell scripts, and you should choose the method that seems most straightforward for you. With that said, let’s take a closer look at the four different methods to run a PowerShell script.
1. Run With PowerShell
The fastest and easiest method to run PowerShell scripts is by right-clicking on them and selecting the “Run with PowerShell” option. However, with this method, the script runs and closes immediately, which means you won’t be able to check for errors or review the output thoroughly. This approach is best suited when you have a script designed for a specific action, such as cleaning temporary files.
READ MORE: How to Fix ‘PowerShell.exe is not a Microsoft-Verified App’ on Windows 10 ➜
2. Using Windows PowerShell
The most preferred method for running PowerShell scripts is by using the command line Windows PowerShell script itself. To run a PowerShell script using Windows PowerShell, follow these steps:
- Open Windows PowerShell as an administrator.
- From here, Navigate to the folder where your script is located using the command — cd “c:\path” (make sure to add the path within the quotation marks). In our case, we have simply saved the script on our Desktop
- Next, enter the name of your script, ensuring you match the case exactly as it appears.
- Finally, hit the Enter key on your keyboard and your script should be executed.
3. Using CMD
Similar to Windows PowerShell, CMD or Command Prompt is also a command-line interface available in Windows. If you prefer CMD more than Windows PowerShell, you can also use it to run your PowerShell scripts. Here’s how to run PowerShell scripts using CMD:
- To run scripts using the command prompt, start by opening the PowerShell executable and then passing the script path as a parameter to it. To do this, head to the Windows Search bar and open Command Prompt (CMD) as an administrator.
- Next, enter PowerShell.exe to launch the PowerShell environments within CMD where you can proceed to execute your scripts by providing their file paths as parameters.
Enter PowerShell.exe - From here, navigate to where your script is located using the command— cd “c:\path“.
Enter your directory - Now, enter the name of your script.
Add your script - Finally, press Enter, and the script will be executed within CMD.
Your script should run right away
READ MORE: Check Windows Last Password Change in CMD or PowerShell ➜
4. Using PowerShell ISE
Windows PowerShell ISE (Integrated Scripting Environment) is an official scripting tool and development environment for PowerShell. It provides a graphical user interface (GUI) that simplifies the creation, debugging, and execution of PowerShell scripts. If you write your scripts or edit others, you’ll probably be using PowerShell ISE.
In that case, here’s a quick rundown to run PowerShell scripts using PowerShell ISE.
- Open up Windows PowerShell ISE using the Windows Search bar and make sure to run it as an administrator.
- Next, click on File and select Open from the drop-down menu. This should open up the File Explorer.
- Here, head to your script’s directory and press the Open button.
- To execute the script, simply click the green run button or press the F5 key on your keyboard. This will run the script within the integrated PowerShell terminal located at the bottom of the window.
READ MORE: How to Check Which Version of PowerShell You’re Running? ➜
Bonus: Creating PowerShell Scripts
Till now, if you have been running PowerShell scripts off of the Internet, it’s time to change that. Creating your PowerShell scripts allows you to create what you need. To create a PowerShell script, you can use two different methods; Notepad and Windows PowerShell ISE. Let’s have a closer look at both of these methods:
1. Using Notepad
- Open Notepad using the Windows Search bar.
- From here, begin by writing your PowerShell commands directly into Notepad. For example, you can start with a simple command like Write-Host “Congratulations! Your first script executed successfully” to see how it works.
- Next, to save your script click on the File option at the top left corner and select Save as from the menu.
- Finally, choose a directory for your script file and name it with a .ps1 extension, such as firstscript.ps1. Once you’ve done this, click on the “Save” button to store your script.
2. Using PowerShell ISE
Another method to creating a PowerShell script is by using the PowerShell ISE editor. To create a PowerShell script using PowerShell ISE, follow these steps:
- Head to the Windows Search bar and open up Windows PowerShell ISE as an administrator.
- Next, press the CTRL + N keys on your keyboard to create a new empty “.ps1” file.
- Now, enter the script you want to run. In our case, we’ll use the same example: “Write-Host ‘Congratulations! Your first script executed successfully”.
- To save the file, click on the File option and select Save as.
- Choose a directory and make sure to add the “.ps1” extension. Finally, hit the Save button and you are good to go.
READ MORE: How to Fix ‘PowerShell.exe is not a Microsoft-Verified App’ on Windows 10 ➜
With that, you have now created your first script. You can run this script using the various methods you’ve learned for executing PowerShell scripts. When creating PowerShell scripts, consider automating tasks you typically perform manually.
For instance, you could write a script to clear out the temp folder, freeing up space taken by unnecessary files.
Get-ChildItem C:\temp | Remove-Item
Similarly, you can check for a registry key on a machine using this PowerShell command.
Get-ItemProperty -Path "HKLM:\SOFTWARE\Admin Arsenal\PDQ Deploy"
These are just a few examples of the many ways PowerShell scripts can help you save time and streamline your work.
Start Your PowerShell Journey
Now that you have learned everything about running PowerShell scripts, it’s time to put your knowledge to the test. Windows PowerShell is a powerful scripting language that introduces a new way to interact with and automate your operating system with just a few simple command lines. So, what are you waiting for? Start running your scripts right away!
FAQs
Command Prompt (CMD) is a basic command-line interface primarily used for executing commands and batch scripts for file management and system tasks. In contrast, Windows PowerShell is a more advanced shell and scripting environment designed for automation, administration, and management of Windows systems.
Parameters are variables or values passed into functions, commands, or scripts to customize how they behave or provide input data. They allow functions and scripts to work with different inputs without needing to change the underlying code structure.
Temporary files are short-term data created by applications and the operating system during normal operations. Regularly cleaning these files is crucial to free up disk space, enhance system performance by reducing clutter, and protect privacy by removing potentially sensitive information. Additionally, clearing temporary files helps prevent issues related to outdated or corrupted data that can impact application functionality and system stability.