How do you write code in PowerShell?

PowerShell is a convenient application used to automate the system’s management, run commands/scripts, tasks automation, etc. It is a cross-platform tool that can run on different operating systems such as Windows, Linux, and Mac. It is composed of a Command shell, configuration management frameworks, and scripting language. It is very much similar to the Windows Command prompt; however, it comes up with some advanced tools/features for example it is available across multiple platforms, it provides task automation solutions, etc.

Considering the key features of PowerShell you must be thinking about getting started with PowerShell. But worried about how to write code/script in PowerShell! If that’s the case then this write-up is going to assist you in this regard from the scratch.

Before moving toward the main topic let’s have a look at the content that we are going to cover in this write-up:

So, let’s begin!

What is a Script?

In PowerShell, a script is a set of instructions stored in a text file with the extension “.ps1”. PowerShell will understand and execute these instructions in a sequence to achieve different functionalities.

How to create a script file?

To write code in PowerShell, we have to create a script file first and then we can write the code within that file. We can create a PowerShell script file using any text editor, or PowerShell ISE. In this write-up, we will create a script in PowerShell ISE.

The below-listed steps will assist you to create a Script File in PowerShell ISE:

Press “Win + R”.
A new window will appear, type “powershell_ise.exe”:

Press “Ok”. Consequently, the following window will appear:

Here, in the scripting pane, we can write the code while the output will be displayed on the console. Moreover, we can run the code/script file from the console. The PowerShell script file will always be saved with the .ps1 extension.

PowerShell set execution policy

If you are running your first program/script in PowerShell then you have to set the execution policy as RemoteSigned. To do that you have to run the PowerShell as administrator as shown in the following snippet:

Next, copy the below-given command and paste it into the Windows PowerShell:

Set-ExecutionPolicy RemoteSigned

Allow the permissions and hit the “Enter” key to set the execution policy to remote signed.

How to run a program/Script in PowerShell?

We can run a script file or code using two different ways i.e. using “console”, and using the “run” button.

How to run a script file or code from the console?

To run a script, we have to type/specify the “.\” followed by the complete path of the file on the console:

Afterwards, press the “Enter” to run the script/code.

How to run a script file or code using the run button?

The second way to run a code is very simple i.e. press the run button as mentioned in the below snippet:

How to write your first program in PowerShell?

The below example will let you understand how to create a simple program in PowerShell ISE:

In this example program we will utilize the “Write-Host” command to print a simple string on the console:

Write-Host "Welcome to linuxhint.com"

This is how we can get started with the PowerShell.


To write code in PowerShell, we have to create a script file first and then we can write the code within that file. A PowerShell script file can be created using any text editor like notepad, VS code, or PowerShell ISE. The script file must be saved with a “.ps1” extension, and it can run from the “console” or using the “run” button. This write-up explained how to create a script file, how to write code in a script file, and how to run a script file using the PowerShell ISE.

About the author

Anees Asghar

I am a self-motivated IT professional having more than one year of industry experience in technical writing. I am passionate about writing on the topics related to web development.