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

What is a Power­Shell script?

Windows Power­Shell is a command-line interface and scripting language designed spe­cific­ally for the ad­min­is­tra­tion and auto­ma­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 Power­Shell script consists of a sequence of Power­Shell cmdlets, functions, variables, con­di­tions and other in­struc­tions to automate processes and recurring tasks. Power­Shell scripts are saved in text files with the file extension .ps1. You can use any text editor or the Power­Shell In­teg­rated Scripting En­vir­on­ment (ISE) console to create them. To execute the script, open the Power­Shell console and enter the path to the script file.

Re­gard­less of how you invoke a Power­Shell script, you should ensure that the script execution policies are properly con­figured in your Power­Shell en­vir­on­ment. By default, Power­Shell script ex­e­cu­tions are disabled on many systems to minimise security risks. You can change the execution policies using the Set-Ex­e­cu­tion­Policy cmdlet.

Dedicated Server
Per­form­ance through in­nov­a­tion
  • En­ter­prise hardware
  • Con­fig­ur­able hardware equipment
  • ISO-certified data centres

How to create a Power­Shell script using Visual Studio Code

Visual Studio Code (VS Code) is a popular, free code editor from Microsoft designed spe­cific­ally for creating scripts and ap­plic­a­tions. It includes powerful syntax high­light­ing for Power­Shell code and supports In­tel­li­S­ense, which enables codes to be completed auto­mat­ic­ally.

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­stall­a­tion.

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

Step 2: Add Power­Shell extension

To be able to use Power­Shell ef­fi­ciently in VS Code, you should install the official Power­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 Power­Shell. Al­tern­at­ively, use the [Ctrl] + [Shift] + [X] shortcut. Install the extension by clicking on Trust Workspace & Install.

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

Step 3: Create Power­Shell script

You can open an existing Power­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 Power­Shell code that can be written to the open file:

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

Step 4: Run Power­Shell script

VS Code contains an in­teg­rated terminal that you can use to execute Power­Shell commands directly in the en­vir­on­ment. Open the terminal by clicking on Terminal > New Terminal and selecting Power­Shell as the terminal type.

Image: VS Code terminal
Select ‘Terminal’ in VS Code

How to create a Power­Shell script using Notepad

In this section, we will show you how to create a Power­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 Power­Shell code

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

Image: Notepad
Paste Power­Shell code into Notepad

Click on File > Save or use the key com­bin­a­tion [Ctrl] + [S]. Enter a file name and add ‘.ps1’ onto the end to save the script as a Power­Shell file. Select a storage location on your computer and click Save.

How to create a Power­Shell script using In­teg­rated Scripting En­vir­on­ment (ISE)

The Power­Shell In­teg­rated Scripting En­vir­on­ment (ISE) is an in­teg­rated de­vel­op­ment en­vir­on­ment (IDE) from Microsoft. Power­Shell ISE is available on Windows systems by default and is a robust and user-friendly en­vir­on­ment for de­vel­op­ing Power­Shell scripts. Note that the ISE has been replaced by Visual Studio Code (VS Code) in Power­Shell 5.0 and higher, as it has ad­di­tion­al features and flex­ib­il­ity.

Step 1: Open Power­Shell ISE

Click on Start at the bottom left of your desktop. Type ‘Power­Shell ISE’ in the search bar and select Run as ad­min­is­trat­or.

Image: PowerShell ISE start
Run Power­Shell ISE as ad­min­is­trat­or

Step 2: Create a new script

In Power­Shell ISE, you can create a new script by clicking on File > New or by using the key com­bin­a­tion [Ctrl] + [N]. Write your Power­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 Power­Shell script:

# This is a comment
$message = "Hello World!"
Write-Host $message
power­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 Power­Shell file.

How to run Power­Shell script

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

Step 1: Start Power­Shell

First open Power­Shell with ad­min­is­trat­or rights as in the ISE example.

Step 2: Change the execution policy

Power­Shell has four different execution policies that control security and the ability to execute scripts in the Power­Shell en­vir­on­ment. The four execution policies are:

  • Re­stric­ted: This is the default execution policy for Power­Shell. With this policy, scripts are disabled, and only in­ter­act­ive 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­ti­fic­ate to sign the script.
  • Re­moteSigned: With Re­moteSigned, only scripts ori­gin­at­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­res­tric­ted: 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­vir­on­ment as it poses a security risk. It should only be con­sidered for testing purposes or in secure en­vir­on­ments.

You can view the current execution policy in your Power­Shell en­vir­on­ment by using the Get-Ex­e­cu­tion­Policy command. To change the execution policy, use the Set-Ex­e­cu­tion­Policy command followed by the policy you want.

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

Set-ExecutionPolicy RemoteSigned
power­shell
Image: PowerShell ISE
Changing the Power­Shell ISE execution policy

Step 3: Confirm execution (if necessary)

Depending on the security settings of your Power­Shell en­vir­on­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 Power­shell script

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

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