- Adam the Automator
- How to Run a PowerShell Script From the Command Line and More
- Parveen Singh
- Prerequisites
- Dealing with the Execution Policy
- Restricted
- Unrestricted
- Remote Signed
- All Signed
- Changing the PowerShell Execution Policy
- Running a PowerShell Script
- Using the Console
- Running a PowerShell Script from the Command Line
- Using the PowerShell ISE
- The Sample Script’s Output
- Running a PowerShell Script from Within a Script
- How to create and run a PowerShell script file on Windows 10
- How to create PowerShell script file on Windows 10
- Creating script with Visual Studio Code
- Install Visual Studio Code
- Install PowerShell extension
- Create PowerShell script with Visual Studio Code
- Creating script with Notepad
- Creating script with Integrated Scripting Environment
- How to run PowerShell script file on Windows 10
- More Windows 10 resources
- The Dell XPS 15 is our choice for best 15-inch laptop
- Halo: MCC’s live service elements make it better, not worse
- Microsoft’s Surface Duo is not ‘failing up’
- These are the best PC sticks when you’re on the move
Adam the Automator
How to Run a PowerShell Script From the Command Line and More
Parveen Singh
Read more posts by this author.
If you’re new to the PowerShell scripting language and want to learn how to run a PowerShell script, you’ve come to the right blog post. This blog will be a tutorial covering common ways to run scripts and a few issues that may pop up.
Table of Contents
Prerequisites
This article will be a walkthrough for you about how to run PowerShell on your local computer. If you’d like to follow along, please be sure you have the following prerequisites in place before starting this article.
- A Windows 10 computer with Administrator privileges.
- Windows PowerShell version 5 or higher. You can also use PowerShell v7. This tutorial will focus on Windows PowerShell since the Windows operating system already has it.
- Any text file editor
Dealing with the Execution Policy
If this is the first time you’re trying to execute a Windows PowerShell script, you may run into a common problem. PowerShell will probably return an error message stating that a script “cannot be loaded because running scripts is disabled on this system”.
PowerShell returns the error message above when you try to run a PowerShell with an execution policy set to Restricted, Remote Signed or All Signed.
Restricted
Restricted is the default policy set for Windows client computers. If you are using PowerShell for the first time, your default policy would probably be set to restrict all the scripts.
You can still execute individual commands in a terminal, but not a script file. The restriction includes any file ending with .ps1xml , .psm1 or .ps1 .
Unrestricted
Unrestricted allows you to run any script however, it warns you before execution if the script is downloaded from the internet. This policy is usually the default for any non-windows devices.
Remote Signed
Remote Signed policy allows you to run any script that is either (a) digitally signed or (b) any script written on your local computer with or without a signature.
If a script is downloaded from the internet and not signed, you would need to unblock the file. You can do so by right-clicking on the file and choosing Properties. Or, you could use the Unblock-File PowerShell cmdlet for that particular script file.
Using a Remote signed policy would be an ideal option when running a script downloaded from the internet.
All Signed
All signed requires all the scripts to be signed digitally by a trusted publisher. This includes the scripts downloaded from the internet and written locally on your computer.
Changing the PowerShell Execution Policy
To change the execution policy:
- Open Windows PowerShell with Run as Administrator to make sure you have the highest permission to make the policy changes.
2. When open, run the following PowerShell command to set your computer’s execution policy. The execution policy, as covered above, can be one of three different types. This tutorial is using a useful yet still secure execution policy of RemoteSigned.
Since this tutorial assumes you’ve downloaded from the Internet the GetServices.ps1 script file, set the execution policy to RemoteSigned.
The RemoteSigned execution policy forces you to cryptographically sign every PowerShell script downloaded from the Internet before PowerShell will run it on your system.
3. You should see an output requesting to confirm the action. Enter Y and hit enter to confirm the policy change.
At this point, follow the next steps to explore different methods to run the PowerShell script on your computer.
Running a PowerShell Script
To demonstrate running a PowerShell script, you actually need a script file to run! If you don’t have one handy, download this ZIP file and extract the PS1 file within. You’ll find a simple script file inside called GetServices.ps1.
Every PowerShell script should end with a .ps1 extension.
Using the Console
Once you have a script ready, there are a few different ways you can execute a PowerShell script file. One of the most common ways is via the PowerShell console.
- Open the PowerShell console as shown above.
2. Navigate to the file system location your script is located using the Set-Location PowerShell cmdlet or the cd alias. This tutorial’s script is found in the C:\Temp directory.
3. Run the script using a dot ( . ) notation. PowerShell is a shell that also looks for command names. To differentiate between a PowerShell command and a script, you must preface the script with a dot. This dot represents the current directory.
Running a PowerShell Script from the Command Line
If you can’t or would rather not run scripts via the PowerShell console, you can also do so with the good ol’ command line (command prompt).
To run scripts via the command prompt, you must first start up the engine (powershell.exe) and then pass the script path as a parameter to it.
You can run scripts with parameters in any context by simply specifying them while running the PowerShell executable like powershell.exe -Parameter ‘Foo’ -Parameter2 ‘Bar’ .
Once you open cmd.exe, you can execute a PowerShell script like below. This example is running the engine and passing it the script path of C:\Temp\GetServices.ps1.
Notice below that the example below is using the full path to run the script. You’ll have to do this if the folder isn’t in your PATH somewhere.
Below is a handy YouTube video that covers executing a script via a batch file which the cmd.exe executes.
Using the PowerShell ISE
If you create your own scripts or edit others’, you’ll probably be using a script editor like the PowerShell ISE or maybe Visual Studio (VS) Code. Since the ISE comes with Windows, let’s focus on that method for this tutorial.
To invoke a script via the ISE:
- Navigate to Start Menu, search for PowerShell ISE and open it.
2. Click on File → Open and find your script.
3. With the script open, click on the green run button to execute the script. This button will invoke the script in the built-in PowerShell terminal at the bottom.
The Sample Script’s Output
A PowerShell script can sometimes return output. This happens when the script you’re executing is built to return objects which is a fundamental component of PowerShell.
If you run the sample GetServices.ps1 script, you will see the following. This script runs the Get-Service cmdlet which returns all of the services installed on your local Windows computer.
Running a PowerShell Script from Within a Script
Let’s say you have two scripts and you’d like one to call the other. Perhaps you have a script called GetUser.ps1 and one called ResetPassword.ps1. Inside of the GetUser.ps1 script, you’d like to execute the ResetPassword.ps1 to reset a user password.
Inside of the calling script (GetUser.ps1), you’d add a line to execute the other script just like you would call the script from the command line.
You can see below you have a couple of options. You should typically choose to run the other script within the same session or scope to simplify things unless you have a specific reason to run the script in another PowerShell session.
How to create and run a PowerShell script file on Windows 10
Source: Windows Central
On Windows 10, PowerShell is a command-line tool designed by Microsoft to run commands and scripts to change settings and automate tasks. In a way, it’s similar to Command Prompt. However, PowerShell is a more capable command-line interface (CLI) that offers an extensive set of tools and more flexibility and control. Also, unlike Command Prompt, PowerShell is available on Windows, macOS, and Linux.
A script is just a collection of commands saved into a text file (using the special «.ps1» extension) that PowerShell understands and executes in sequence to perform different actions.
The only caveat is that the default security protocol always blocks any script from running on a device. This means that when double-clicking a «.ps1» file on Windows 10 nothing will happen, and if you try to run the script within PowerShell, you’ll see the «cannot be loaded because running scripts is disabled on this system» error message. However, it’s not impossible to run scripts on your computer. You only need to enable the correct execution policy.
In this Windows 10 guide, we’ll walk you through the steps to successfully write and run your first script file on PowerShell using Visual Studio Code, Notepad, and the PowerShell Integrated Scripting Environment (ISE) console.
How to create PowerShell script file on Windows 10
On Windows 10, you can create PowerShell script files using virtually any text editor or the ISE console. However, the preferred option (thanks @jotaka for the heads up) to build scripts moving forward is to use the Visual Studio Code editor with the PowerShell extension.
Creating script with Visual Studio Code
Visual Studio Code — also known as VS Code — is a free and extensible cross-platform code editor that provides an environment to edit virtually any kind of programming language. And when adding the PowerShell extension, you get a fully interactive scripting editing experience, even with IntelliSense (code-completion) support.
The new experience is meant to be the new default, but the PowerShell ISE console isn’t going away. Still, the company won’t be adding any more features, and it doesn’t support PowerShell 7 or higher releases.
Install Visual Studio Code
To install Visual Basic Code on Windows 10, use these steps:
Click the Windows button to download the installer.
Source: Windows Central
Click the Next button.
Source: Windows Central
Confirm additional tasks as necessary.
Source: Windows Central
Once you complete the steps, you can proceed to install the PowerShell extension.
Install PowerShell extension
To install the PowerShell extension on VS Code, use these steps:
- Open VS Code.
- Click the Extensions tab from the left pane.
- Search for PowerShell and select the top result.
Click the Install button.
Source: Windows Central
After you complete the steps, you can start writing PowerShell scripts using Visual Studio Code.
Create PowerShell script with Visual Studio Code
To create a script with Visual Basic Code, use these steps:
- Open VS Code.
Click the File menu and select the New File option.
Source: Windows Central
Click the File menu and select the Save as option.
Source: Windows Central
Write a new, or paste the script you want to run — for example:
Write-Host «Congratulations! Your first script executed successfully»
The above script will output the phrase «Congratulations! Your first script executed successfully» on the screen.
(Optional) Click the Run button from the top-right side (or press the F5 key) to run the script.
Source: Windows Central
Creating script with Notepad
To create a PowerShell script using the Notepad editor on Windows 10, use these steps:
- Open Start.
- Search for Notepad, and click the top result to open the app.
Write a new, or paste your script, in the text file — for example:
Write-Host «Congratulations! Your first script executed successfully»
Source: Windows Central
Type a descriptive name for the script — for example, first_script.ps1.
Source: Windows Central
Creating script with Integrated Scripting Environment
Alternatively, you can use the built-in PowerShell ISE console to code your scripts on Windows 10.
The Integrated Scripting Environment is an advanced tool, but you can get started using these steps:
- Open Start.
- Search for Windows PowerShell ISE, right-click the top result, and select the Run as administrator option.
- Click on File menu.
Select the New option to create a new empty .ps1 file.
Source: Windows Central
Write a new, or paste the script you want to run — for example:
Write-Host «Congratulations! Your first script executed successfully»
Source: Windows Central
Type a name for the script – for example, first_script.ps1.
Source: Windows Central
Once you complete the steps using Notepad, Visual Studio Code, or PowerShell ISE, the script will be ready to run, but it will fail by default. This is because the default PowerShell settings are always set to block the execution of any script. (The only exception is if you run the contents of the script within Visual Studio Code or PowerShell ISE.)
How to run PowerShell script file on Windows 10
If you wish to run a script file with PowerShell, you have to change the execution policy on Windows 10.
To change the execution policy to run PowerShell scripts, use these steps:
- Open Start.
- Search for PowerShell, right-click the top result, and select the Run as administrator option.
Type the following command to allow scripts to run and press Enter:
Type A and press Enter (if applicable).
Source: Windows Central
Type the following command to run the script and press Enter:
In the above command, make sure to change «PATH\TO\SCRIPT» to the location of your script.
For example, this command runs a script stored in the Downloads folder:
Source: Windows Central
After you complete the steps, the script will run, and if it was written correctly, you should see its output without issues.
On Windows 10, PowerShell includes four execution policies, including:
- Restricted — Stops any script from running.
- RemoteSigned — Allows scripts created on the device, but scripts created on another computer won’t run unless they include a trusted publisher’s signature.
- AllSigned — All the scripts will run, but only if a trusted publisher has signed them.
- Unrestricted — Runs any script without any restrictions.
In the above steps, we use the command to allow local scripts to run on Windows 10. However, if you’re not planning to run scripts regularly, you can restore the default settings to block untrusted scripts using the same instructions outlined above, but on step No. 4, make sure to use the Set-ExecutionPolicy Restricted command.
More Windows 10 resources
For more helpful articles, coverage, and answers to common questions about Windows 10, visit the following resources:
The Dell XPS 15 is our choice for best 15-inch laptop
For a lot of people, a 15-inch laptop is a perfect size that offers enough screen for multitasking, and in a lot of cases, some extra performance from powerful hardware. We’ve rounded up the best of the best at this size.
Halo: MCC’s live service elements make it better, not worse
Halo: The Master Chief Collection is more popular than ever, but some fans don’t agree with the live service approach 343 Industries has taken with it. Here’s why those elements are, at the end of the day, great for the game and for Halo overall.
Microsoft’s Surface Duo is not ‘failing up’
Microsoft announced this week that it was expanding Surface Duo availability to nine new commercial markets. While Surface Duo is undoubtedly a work in progress, this is not a sign of a disaster. It’s also doesn’t mean that Surface Duo is selling a ton either. Instead, the reason for the expansion is a lot more straightforward.
These are the best PC sticks when you’re on the move
Instant computer — just add a screen. That’s the general idea behind the ultra-portable PC, but it can be hard to know which one you want. Relax, we have you covered!