With Windows Pow­er­Shell, you can perform various system tasks by entering commands in a text interface.

What is a Pow­er­Shell script?

Windows Pow­er­Shell is a command-line interface and scripting language designed specif­i­cal­ly for the ad­min­is­tra­tion and au­toma­tion of Windows operating systems and related Microsoft products. It was first published in 2006 and has since played an important role in the system ad­min­is­tra­tion of Windows systems.

A Pow­er­Shell script consists of a sequence of Pow­er­Shell cmdlets, functions, variables, con­di­tions and other in­struc­tions to automate processes and recurring tasks. Pow­er­Shell scripts are saved in text files with the file extension .ps1. You can use any text editor or the Pow­er­Shell In­te­grat­ed Scripting En­vi­ron­ment (ISE) console to create them. To execute the script, open the Pow­er­Shell console and enter the path to the script file.

Re­gard­less of how you invoke a Pow­er­Shell script, you should ensure that the script execution policies are properly con­fig­ured in your Pow­er­Shell en­vi­ron­ment. By default, Pow­er­Shell script ex­e­cu­tions are disabled on many systems to minimize security risks. You can change the execution policies using the Set-Ex­e­cu­tion­Pol­i­cy cmdlet.

Dedicated Servers
Per­for­mance through in­no­va­tion
  • Dedicated en­ter­prise hardware
  • Con­fig­urable hardware equipment
  • ISO-certified data centers

How to create a Pow­er­Shell script using Visual Studio Code

Visual Studio Code (VS Code) is a popular, free code editor from Microsoft designed specif­i­cal­ly for creating scripts and ap­pli­ca­tions. It includes powerful syntax high­light­ing for Pow­er­Shell code and supports In­tel­liSense, which enables codes to be completed au­to­mat­i­cal­ly.

Step 1: Install VS Code

If you don’t have VS Code installed, you can download it from the official website. Select Windows as the operating system and double-click to start the in­stal­la­tion.

Image: Web page for VS Code download
Download VS Code for Windows

Step 2: Add Pow­er­Shell extension

To be able to use Pow­er­Shell ef­fi­cient­ly in VS Code, you should install the official Pow­er­Shell extension from Microsoft. To do this, go to the Ex­ten­sions area in VS Code (icon in the left sidebar) and search for Pow­er­Shell. Al­ter­na­tive­ly, use the [Ctrl] + [Shift] + [X] shortcut. Install the extension by clicking on Trust Workspace & Install.

Image: Detail page for PowerShell extension
Install the Pow­er­Shell extension for VS Code

Step 3: Create Pow­er­Shell script

You can open an existing Pow­er­Shell script or create a new one by selecting File > New Text File and then entering the file name including the extension “.ps1” under Save As.

Here is an example of Pow­er­Shell code that can be written to the open file:

# This is a comment
$message = "Script to be executed"
Write-Host $message
pow­er­shell

Step 4: Run Pow­er­Shell script

VS Code contains an in­te­grat­ed terminal that you can use to execute Pow­er­Shell commands directly in the en­vi­ron­ment. Open the terminal by clicking on Terminal > New Terminal and selecting Pow­er­Shell as the terminal type.

Image: VS Code terminal
Select “Terminal” in VS Code

How to create a Pow­er­Shell script using Notepad

In this section, we will show you how to create a Pow­er­Shell script using the Notepad text editor.

Step 1: Open Notepad

Click on Start or on the Windows icon at the bottom left of your desktop. Enter “Notepad” in the search bar and press the Enter key. This will open the Notepad text editor.

Step 2: Write Pow­er­Shell code

You can paste the Pow­er­Shell code directly into the Notepad editor.

Image: Notepad
Paste Pow­er­Shell code into Notepad

Click on File > Save or use the key com­bi­na­tion [Ctrl] + [S]. Enter a file name and add “.ps1” onto the end to save the script as a Pow­er­Shell file. Select a storage location on your computer and click Save.

How to create a Pow­er­Shell script using In­te­grat­ed Scripting En­vi­ron­ment (ISE)

The Pow­er­Shell In­te­grat­ed Scripting En­vi­ron­ment (ISE) is an in­te­grat­ed de­vel­op­ment en­vi­ron­ment (IDE) from Microsoft. Pow­er­Shell ISE is available on Windows systems by default and is a robust and user-friendly en­vi­ron­ment for de­vel­op­ing Pow­er­Shell scripts. Note that the ISE has been replaced by Visual Studio Code (VS Code) in Pow­er­Shell 5.0 and higher, as it has ad­di­tion­al features and flex­i­bil­i­ty.

Step 1: Open Pow­er­Shell ISE

Click on Start at the bottom left of your desktop. Type “Pow­er­Shell ISE” in the search bar and select Run as ad­min­is­tra­tor.

Image: PowerShell ISE start
Run Pow­er­Shell ISE as ad­min­is­tra­tor

Step 2: Create a new script

In Pow­er­Shell ISE, you can create a new script by clicking on File > New or by using the key com­bi­na­tion [Ctrl] + [N]. Write your Pow­er­Shell code in the main window of the ISE. You have access to features such as syntax high­light­ing, automatic code com­ple­tion and a clear user interface that makes script de­vel­op­ment easier.

Here is an example of a simple Pow­er­Shell script:

# This is a comment
$message = "Hello World!"
Write-Host $message
pow­er­shell

Step 3: Save the script

Click on File > Save or press [Ctrl] + [S]. Make sure you add “.ps1” on the end to save the script as a Pow­er­Shell file.

How to run Pow­er­Shell script

A Pow­er­Shell script is usually started via the Pow­er­Shell console or another terminal.

Step 1: Start Pow­er­Shell

First open Pow­er­Shell with ad­min­is­tra­tor rights as in the ISE example.

Step 2: Change the execution policy

Pow­er­Shell has four different execution policies that control security and the ability to execute scripts in the Pow­er­Shell en­vi­ron­ment. The four execution policies are:

  • Re­strict­ed: This is the default execution policy for Pow­er­Shell. With this policy, scripts are disabled, and only in­ter­ac­tive commands can be executed in the console. Since this prevents all scripts from running, it provides the highest level of security.
  • AllSigned: With this policy, all scripts must be digitally signed in order to be run. This means that the author of the script must use a digital cer­tifi­cate to sign the script.
  • Re­mote­Signed: With Re­mote­Signed, only scripts orig­i­nat­ing from the internet or from a network location must be signed. Local scripts that are stored on your computer can be executed without a signature. This makes it easier to use local scripts.
  • Un­re­strict­ed: This policy allows the execution of all scripts without signature or re­stric­tion. It is strongly dis­cour­aged to use this policy in a pro­duc­tion en­vi­ron­ment as it poses a security risk. It should only be con­sid­ered for testing purposes or in secure en­vi­ron­ments.

You can view the current execution policy in your Pow­er­Shell en­vi­ron­ment by using the Get-Ex­e­cu­tion­Pol­i­cy command. To change the execution policy, use the Set-Ex­e­cu­tion­Pol­i­cy command followed by the policy you want.

You can enter the following command to allow scripts to be executed in Pow­er­Shell:

Set-ExecutionPolicy RemoteSigned
pow­er­shell
Image: PowerShell ISE
Changing the Pow­er­Shell ISE execution policy

Step 3: Confirm execution (if necessary)

Depending on the security settings of your Pow­er­Shell en­vi­ron­ment, you may receive a security prompt to confirm that you want to run the script. Enter “Y” or “A” to agree, or “N” if you don’t want to run it.

Step 4: Run Pow­er­shell script

To run the Pow­er­Shell script, insert the path to the file:

& "C:\PATH\TO\SCRIPT\script.ps1"
pow­er­shell
Go to Main Menu