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

PowerShell script
PowerShell script (Image credit: Mauro Huculak)

On Windows 11 (or 10), PowerShell is a command-line interface (CLI) to run commands and scripts to automate tasks and change settings on your device, similar to Command Prompt. However, PowerShell is a more powerful CLI, offering more tools and flexibility. In addition, unlike Command Prompt, PowerShell is available across platforms, including Windows, macOS, and Linux.

A script is a collection of instructions written to a text file (using the ".ps1" extension) that PowerShell understands and executes in sequence to run different actions.

The only caveat is that the default security protocol prevents scripts from running on your computer. This means that when double-clicking a ".ps1" file, the system won't do anything, and if you try to run the script within PowerShell, you will see the "cannot be loaded because running scripts is disabled on this system" error message. However, running scripts on your device is possible. You only need to enable the correct execution policy.

In this how-to guide, I will walk you through the steps to create and run your first script file on PowerShell using Visual Studio Code, Notepad, and the PowerShell Integrated Scripting Environment (ISE) console, whether you use Windows 10 or 11.

How to create PowerShell script file on Windows 11 and 10

It's possible to create PowerShell script files using any text editor or the legacy ISE application. However, the Visual Studio Code editor is the preferred option for writing scripts moving forward.

Create a script with VS Code

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

You can still use PowerShell ISE, but 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 future releases.

Install VS Code

To install Visual Basic Code on Windows 11 (or 10), use these steps:

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

(Image credit: Mauro Huculak)
  1. Double-click the installer to begin the process.
  2. Select the "I accept the agreement" option.
  3. Click the Next button.
  4. Confirm additional tasks as necessary.
  5. Click the Next button again.

(Image credit: Mauro Huculak)
  1. Click the Install button.
  2. Click the Finish button.

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

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" keyboard shortcut) from the left pane.
  3. Search for PowerShell and select the top result.
  4. Click the Install button.

(Image credit: Mauro Huculak)
  1. Click the "Trust Workspace & Install" button.

(Image credit: Mauro Huculak)

After you complete the steps, you can start writing PowerShell scripts from Visual Studio Code.

Create PowerShell script with Visual Studio Code

To create a script with Visual Basic Code on Windows 11 (or 10), use these steps:

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

(Image credit: Mauro Huculak)
  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 on the screen that says: "Congratulations! Your first script executed successfully."
  1. (Optional) Open the Run menu, select the Start Debugging option from the command bar, or press the F5 key) to run the script.

(Image credit: Mauro Huculak)
  1. Click the File menu.
  2. Click the Save option.

Create PowerShell script with Notepad

To create a PowerShell script with the Notepad app, 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"

(Image credit: Mauro Huculak)
  • Quick note: Although the Notepad app looks a little different on Windows 11, the instructions will also apply.
  1. Click the File menu.
  2. Select the Save As option.
  3. Confirm a descriptive name for the script — for example, first_script.ps1.

(Image credit: Mauro Huculak)
  1. Click the Save button.

Create PowerShell script with Integrated Scripting Environment

You can also use the built-in PowerShell ISE app to code your scripts on Windows.

The Integrated Scripting Environment (ISE) is a complex tool, but you can get started using these easy 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.
  5. Write a new script or paste the script you want to run — for example, Write-Host "Congratulations! Your first script executed successfully"

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

(Image credit: Mauro Huculak)
  1. Select the folder to save the script file.
  2. Click the Save button.
  3. (Optional) Click the Run button on 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 due to 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 11 and 10

Whether you use Windows 11 or 10 on your computer, you must change the execution policy to run a script with PowerShell.

To change the execution policy to run PowerShell scripts on Windows 11 (or 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"

(Image credit: Mauro Huculak)

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.

PowerShell includes four execution policies that apply to the operating system:

  • Restricted — Prevents any script from running.
  • RemoteSigned — Allows scripts created on the computer, but scripts created on another device 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.

It's recommended to allow local scripts to run a script only from a trusted source. If you don't plan to run scripts regularly, it's best to restore the default settings to block untrusted scripts using the same instructions outlined above, but in step 4, use the "Set-ExecutionPolicy Restricted" command.

More resources

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

Mauro Huculak

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.