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

A script is a collection of commands stored in a text file (using the special extension .ps1) that PowerShell understands and executes step by step to trigger a variety of operations.

In this article, TipsMake will guide you how to create and run a PowerShell script file on Windows 10.

PowerShell is a command line tool designed by Microsoft to run commands and scripts to change settings or automate tasks. It sounds like Command Prompt. However, PowerShell is a more functional command line interface (CLI) with a variety of toolkits and flexible control and control capabilities. 

While Command Prompt is only available on Windows, PowerShell is available on Windows, macOS, and Linux.

To create and run a PowerShell script file on Windows 10, follow these steps:

How to create a PowerShell script file

On Windows 10, you can virtually create a PowerShell script file using any text editor or ISE (Integrated Scripting Environment). However, it's best to use the Visual Sturio Code editor with the PowerShell extension.

Visual Studio Code - also known as VS Code - is a free and extensible cross-platform code editor. With VS Code, we can compose and edit almost any kind of programming language. And when you add the PowerShell extension, you'll have a fully interactive scripting experience.

So, the first thing you need to do is download and install VS Code:

  1. Visit Microsoft's VS Code download page here.
  2. Click the Windows button to download the installer
  3. Double click the left mouse button on the downloaded file to run the VS Code installer
  4. Agree to the terms and conditions
  5. Click Next
  6. Click Next again
  7. Click Next for the third time
  8. Confirm additional actions
  9. Click Next
  10. Click Install
  11. Wait for the loader to finish and click Finish

After downloading and installing VS Code, you need to install the PowerShell extension. Here's how to do it:

  1. Open VS Code
  2. Click the Extensions tab on the left side or click Ctrl + Shift + X
  3. Search for PowerShell and click on the first result
  4. Click the Install button

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

When you're done, you can start writing the PowerShell script file using VS Code:

  1. Open VS Code
  2. Click the File menu and choose New File
  3. Click the File menu and choose Save as
  4. In the File name field , name the file with the .ps1 extension , for example make tech easier.
  5. Click Save

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

You can write or paste the script you want to run, for example:

Write-Host "Welcome tipsmake script"

The script above when running will display the following text below the screen:

Welcome tipsmake script

You can run the script by pressing the Run button on the upper right side or pressing the F5 shortcut . After writing the script you should press the File menu and choose Save to save.

After you have finished, your script should be able to run but not execute your commands. This is because by default PowerShell is always set to block any script action. The only exception is when you run the script in VS Code or PowerShell ISE.

How to run a PowerShell script file on Windows 10

To run a script file with PowerShell you need to change the execution policy on Windows 10. 

On Windows 10, PowerShell includes 4 enforcement policies including:

  1. Restricted - Do not allow any scripts to run
  2. RemoteSigned - Allows you to run scripts created on the device but block scripts created on other computers unless they are signed by a trusted entity.
  3. AllSigned - All scripts will run when they are signed by a trusted entity
  4. Unrestricted - Run any script on the computer, without any restrictions

Here's how you set up to run PowerShell scripts on Windows 10:

  1. Press Windows to open the Start menu
  2. Type powershellthen right-click on the search results to run  PowerShell as Administrator
  3. In the PowerShell window , enter the following command and then press Enter :
Set-ExecutionPolicy RemoteSigned
  1. Type A and press Enter

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

  1. Next, you use the following command to run the script. Remember to change the path and filename of the script file to suit your needs:
& "C:/PATHTOSCRIPT/tipsmake_script.ps1"

Above is how to create and run PowerShell script files on Windows 10. Wish you success and invite you to read more great tips on our site.

Update 18 January 2021
Category

System

Mac OS X

Hardware

Game

Tech info

Technology

Science

Life

Application

Electric

Program

Mobile