Skip to main content

How to create and run a PowerShell script file on Windows 10

PowerShell run script on Windows 10
PowerShell run script on Windows 10 (Image credit: Windows Central)

On Windows 10, PowerShell is a tool designed to run commands and scripts to change settings, automate tasks, and other actions. In a way, it's similar to Command Prompt. However, PowerShell is a more capable command-line interface (CLI) that offers a more extensive set of tools and more flexibility and control. Also, unlike Command Prompt, PowerShell is available across platforms, including Windows, macOS, and Linux.

A script is a collection of instructions 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 scripts from running on your computer. 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, running scripts on a laptop or desktop computer is not impossible. You only need to enable the correct execution policy.

In this Windows 10 guide, we will 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

You can create PowerShell script files using virtually any text editor or the legacy ISE console. However, the preferred option (thanks, @JotaKa, for the tip) to write scripts is using the Visual Studio Code editor with the PowerShell extension.

Create script with Visual Studio Code

Visual Studio Code (VS Code) is a free and extensible cross-platform code editor that allows you to edit virtually any programming language. And when adding the PowerShell extension, you get an interactive scripting editing experience, even with IntelliSense (code-completion) support.

The PowerShell ISE application will continue to be available, but the Visual Studio Code with the PowerShell extension is meant to be the new default experience. Also, consider that the legacy experience won't get any new features and doesn't support PowerShell 7 or higher releases.

Install Visual Studio Code

To install Visual Basic Code on Windows 10, use these steps:

  1. Open Visual Studio Download page.
  2. Click the Windows button to download the installer.

Source: Windows Central (Image credit: Source: Windows Central)
  1. Double-click the installer to begin the installation process.
  2. Confirm the agreement terms.
  3. Click the Next button.

Source: Windows Central (Image credit: Source: Windows Central)
  1. Click the Next button again.
  2. Click the Next button one more time.
  3. Confirm additional tasks as necessary.

Source: Windows Central (Image credit: Source: Windows Central)
  1. Click the Next button.
  2. Click the Install button.
  3. Click the Finish button.

Once you complete the steps, you can continue installing the PowerShell extension.

Install PowerShell extension

To install the PowerShell extension on VS Code, use these steps:

  1. Open VS Code.
  2. Click the Extensions tab (Ctrl + Shift + X) from the left pane.
  3. Search for PowerShell and select the top result.
  4. Click the Install button.

Source: Windows Central (Image credit: Source: Windows Central)
  1. Click the Trust Workspace & Install button.

After you complete the steps, you can start writing PowerShell scripts using Visual Studio Code on Windows 10.

Create PowerShell script with Visual Studio Code

To create a script with Visual Basic Code, use these steps:

  1. Open VS Code.
  2. Click the File menu and select the New File option.
  3. Click the File menu and select the Save As option.

Source: Windows Central (Image credit: Source: Windows Central)
  1. In the "File name" field specify a name for the file with the .ps1 extension — for example: first_script.ps1.
  2. Click the Save button.
  3. Write a new or paste the script you want to run — for example:Write-Host "Congratulations! Your first script executed successfully"Quick note: The above script will output the phrase "Congratulations! Your first script executed successfully" on the screen.
  4. (Optional) Click the Run button from the top-right side (or press the F5 key) to run the script.

Source: Windows Central (Image credit: Source: Windows Central)
  1. Click the File menu.
  2. Click the Save option.

Create PowerShell script with Notepad

To create a PowerShell script using the Notepad editor on Windows 10, use these steps:

  1. Open Start.
  2. Search for Notepad, and click the top result to open the app.
  3. Write a new or paste your script in the text file — for example:Write-Host "Congratulations! Your first script executed successfully"

Source: Windows Central (Image credit: Source: Windows Central)
  1. Click the File menu.
  2. Select the Save As option.
  3. Confirm a descriptive name for the script — for example, first_script.ps1.

Source: Windows Central (Image credit: Source: Windows Central)
  1. Click the Save button.

Create PowerShell 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:

  1. Open Start.
  2. Search for Windows PowerShell ISE, right-click the top result, and select the Run as administrator option.
  3. Click the File menu.
  4. Select the New option to create a new empty .ps1 file.

Source: Windows Central (Image credit: Source: Windows Central)
  1. Write a new, or paste the script you want to run — for example:Write-Host "Congratulations! Your first script executed successfully"

Source: Windows Central (Image credit: Source: Windows Central)
  1. Click the File menu.
  2. Click the Save option.
  3. Type a name for the script — for example, first_script.ps1.

Source: Windows Central (Image credit: Source: Windows Central)
  1. Select the folder location to store the script file.
  2. Click the Save button.
  3. (Optional) Click the Run button from the top-right side (or press the F5 key) to run the script.

Once you complete the steps using Notepad, Visual Studio Code, or PowerShell ISE, the script will be ready to run, but it will fail using the default system settings. The reason is that the default PowerShell settings are configured to block the execution of any script. (The only exception is if you run the script's contents within Visual Studio Code or PowerShell ISE.)

How to run PowerShell script file on Windows 10

On Windows 10, to run a script file with the PowerShell console, you have to change the execution policy.

To change the execution policy to run PowerShell scripts on Windows 10, use these steps:

  1. Open Start.
  2. Search for PowerShell, right-click the top result, and select the Run as administrator option.
  3. Type the following command to allow scripts to run and press Enter:Set-ExecutionPolicy RemoteSigned
  4. Type A and press Enter (if applicable).
  5. Type the following command to run the script and press Enter:& "C:\PATH\TO\SCRIPT\first_script.ps1"In the above command, change "PATH\TO\SCRIPT" to the location of your script.For example, this command runs a script stored in the Downloads folder:& "C:\Users\username\Downloads\first_script.ps1"

Source: Windows Central (Image credit: Source: Windows Central)

After you complete the steps, the script will run, and if it was written correctly, you should see its output on the screen without issues.

On Windows 10, PowerShell includes four execution policies:

  • 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.

You should only allow local scripts when you need to run a script from a trusted source. If you don't plan to run scripts regularly, it's a good idea to restore the default settings to block untrusted scripts using the same instructions outlined above, but on step 4, use the

Set-ExecutionPolicy Restricted

command.

More Windows resources

For more helpful articles, coverage, and answers to common questions about Windows 10 and Windows 11, visit the following resources:

Mauro Huculak is technical writer for WindowsCentral.com. His primary focus is to write comprehensive how-tos to help users get the most out of Windows 10 and its many related technologies. He has an IT background with professional certifications from Microsoft, Cisco, and CompTIA, and he's a recognized member of the Microsoft MVP community.

9 Comments
  • Nice article. However, you may want to mention that VS Code and the PowerShell Extension is now the official PowerShell editor on Windows, Linux, and Mac OS.
  • You really need to like torturing yourself to use powershell for anything useful... I feel powershell was an experimental research project that microsoft never finished... and should really be tossed and rewritten based on what they learnt from it... it should have never been a release to thepublic as the default shell in my opinion... it’s too rough and half finished and too many bad ideas that didn’t work well that need reworked... and the way they fix them with all the dumb aliases... it all seem ... well dumb... it’s just a research project to discover a better shell not the end shell that everybody wants ... why is Microsoft continuing to torture everybody with this crap?
  • Any decent Administrator should be comfortable with PowerShell. In fact, even a crap IT Admin should understand the absolute basics.
  • You obviously fail at grasping PS because it works just like any other object oriented language. There are modules in PS just like C# or C++ has APIs. It is very well put together IMO yes things get depricated and rewritten just like any other language as things getting better over time. It is great because you can use it to manage nearly anything in Windows, Azure, or O365 and it makes it great to have a single scripting language for everything.
  • Could anyone please tell me what the difference is between choosing "A" (Yes to All) or "Y" (Yes) when changing policies? As I understand it, either will OK running scripts created on the device with RemoteSigned, so just wondering the difference. Also wondering why it's call "RemoteSigned" when it's only to run scripts signed by the local device. Seems like it should be "LocalSigned". Thank you! :)
  • Think of yes vs all like run once vs always allow respectively. The remote signed option allows any signed PS script to run. Locally/domain signed doesn't need to be allowed since the certs are trusted already.
  • A or Y doesn't matter in this instance because there's only one confirmation prompt. A essentially sets $confirm to $false so any subsequent confirmations prompts are suppressed as if you had selected Y for them as well. RemoteSigned means remote scripts must be signed. If the script originates from your computer then it won't require signing to run otherwise they must be signed and thus cannot be easily altered and as long as you trust the signing certificate you can more or less trust the authenticity of a script. For example, if you download a script from the internet you can trust it hasn't been tampered with without needing to go through all the code if you trust the publisher. By default Restricted doesn't let ANY scripts run.
    The next less restrictive level is AllSigned which requires ALL scripts to be signed. This is arguably most secure setting that allows scripting but then you must ensure all scripts run (and that other 3rd party tools may use) are all signed which may be prohibitive or increase overhead. RemoteSigned could also be thought of as AllowLocal if you wanted to think about it locally.
  • I know this is on me, but even when I'm working in PS as a shell, I'll routinely type CMD to get to an old-fashioned command prompt, because that's still much easier (for me anyway) to do bulk renames (ren *.txt new-name*.txt) and for loops on simple commands. Maybe I just need to get more familiar with PS, but it seems there remain opportunities for MS to make those common tasks as easy to do in PS as they were from the old command prompt. On the other hand, for Active Directory and device management, PS is just fantastic.
  • Visual Studio uses Microsoft software development platforms such as Windows API, Windows Forms, Windows Presentation Foundation, Windows Store, and Microsoft Silverlight. It can produce both native code and managed code. Visual Studio at crackplease includes a code editor that supports IntelliSense (the code completion component) as well as code refactoring.