How to create and run PowerShell script files on Windows 10 is simple and effective

How to create and run PowerShell script files on Windows 10 is simple and effective

A script is a collection of commands stored in a text file (using the special extension .ps1) that PowerShell understands and executes in steps to trigger various operations. In this article, make tech easier to guide you how to create and run PowerShell script files 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:

  • Visit Microsoft’s VS Code download page
  • Click the Windows button to download the installer
  • Double click the left mouse button on the downloaded file to run the VS Code installer
  • Agree to the terms and conditions
  • Click Next
  • Click Next again
  • Click Next for the third time
  • Confirm additional actions
  • Click Next
  • Click Install
  • 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:

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

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

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

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

Write-Host "Chào mừng bạn tới với Quantrimang! Script đầu tiên của bạn đã khởi chạy thành công!"

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

Chào mừng bạn tới với Quantrimang! Script đầu tiên của bạn đã khởi chạy thành công!

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:

  • Restricted – Do not allow any scripts to run
  • 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.
  • AllSigned – All scripts will run when they are signed by a trusted entity
  • Unrestricted – Run any script on the computer, without any restrictions

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

  • Press Windows to open the Start menu
  • Type powershell then right-click on the search result to run PowerShell as Administrator
  • In the PowerShell window, enter the following command and then press Enter :
Set-ExecutionPolicy RemoteSigned
  • Type A and press Enter

  • Next, you use the following command to run the script. Remember to change the path and name of the script file to suit your needs:
& "C:PATHTOSCRIPTQuantrimang_script.ps1"

Above is how to create and run PowerShell script files on Windows 10 that make tech easier to introduce to you. I wish you success and invite you to read more great tips on our site: