PowerShell run script on Windows 10Source: 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.

Prime Day may have ended, but these 25 deals are still available now!

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:

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

    Visual Studio Code downloadSource: Windows Central

  3. Double-click the downloaded file to start the installation process of VS Code.
  4. Confirm the agreement terms.
  5. Click the Next button.

    Setup VS CodeSource: Windows Central

  6. Click the Next button again.
  7. Click the Next button one more time.
  8. Confirm additional tasks as necessary.

    Visual Studio Code additional settingsSource: Windows Central

  9. Click the Next button.
  10. Click the Install button.
  11. Click the Finish button.

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:

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

    Install Powershell Extension on VS CodeSource: 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:

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

    VS Code create new fileSource: Windows Central

  3. Click the File menu and select the Save as option.

    VS Code create new ps1 fileSource: Windows Central

  4. In the "File name" field specify a name for the file with the .ps1 extension — for example: first_script.ps1.
  5. Click the Save button.
  6. 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.

  7. (Optional) Click the Run button from the top-right side (or press the F5 key) to run the script.

    Run PowerShell script on Visual Studio CodeSource: Windows Central

  8. Click the File menu.
  9. Click the Save option.

Creating 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"

    PowerShell notepad scriptSource: Windows Central

  4. Click the File menu.
  5. Select the Save As option.
  6. Type a descriptive name for the script — for example, first_script.ps1.

    Notepad ps1 Powershell script fileSource: Windows Central

  7. Click the Save button.

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:

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

    PowerShell create PS1 file with ISESource: Windows Central

  5. Write a new, or paste the script you want to run — for example:

    Write-Host "Congratulations! Your first script executed successfully"

    PowerShell ISE simple scriptSource: Windows Central

  6. Click the File menu.
  7. Click the Save option.
  8. Type a name for the script – for example, first_script.ps1.

    PowerShell ISE save script as ps1Source: Windows Central

  9. Select the folder location to store the script.
  10. Click the Save button.

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:

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

    PowerShell enable policy to run scriptSource: Windows Central

  5. Type the following command to run the script and press Enter:

    & "C:\PATH\TO\SCRIPT\first_script.ps1"

    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:

    & "C:\Users\username\Downloads\first_script.ps1"

    Run PowerShell script on Windows 10Source: 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:

We may earn a commission for purchases using our links. Learn more.