Powershell

How to create a PowerShell script?

PowerShell is a scripting language tool mainly used for the automation and configuration management of Windows. Some Windows users perceive that Command Prompt and PowerShell are the same. However, PowerShell is the most advanced version of Command Prompt. Its scripts can be created, used, and executed using the PowerShell ISE host application.

This post will guide about:

What is PowerShell Scripting?

PowerShell script is simply a text file or document with a “.ps1” extension that contains PowerShell commands stored in it. These scripts are executed line by line in sequence by the interpreter. PowerShell scripting is very powerful as it helps to perform administrative tasks such as adding or removing users on Windows.

Moreover, scripting has become easier with the PowerShell ISE tool, included in the latest version of Windows. It can create automated tasks to update the security software or allow other users to share files.

What are the Basics of PowerShell?

Before using PowerShell, you must have a basic knowledge of its cmdlets, functions, and aliases which are given below:

Terms Description
cmdlet A cmdlet exists as a script that can be executed to perform different operations, such as copying a file or deleting a file. Its examples include Get-Help, Get-ChildItem, or Copy-Item.
aliases An alias is the second name for the cmdlets. It is used to assign another name to a certain cmdlet. As a result, the particular cmdlet will be invoked using the new name assigned.
functions A function consists of the PowerShell statements. It can be invoked using the function name assigned by the user.

How to Create a PowerShell Script?

The script in PowerShell can be created using these given approaches:

  • PowerShell ISE (Integrated Scripting Environment)
  • Text Editor
  • Third-party IDE (Visual Studio)

Method 1: Creating a PowerShell Script Using PowerShell ISE (Integrated Scripting Environment)

The PowerShell ISE is ideal when it comes to creating scripts, as it is solely designed for scripting purposes. It is similar to PowerShell but with an additional feature of creating and running scripts.

The procedure to create a script in PowerShell is provided below.

Step 1: Launch PowerShell ISE

First, navigate to the Start menu and launch “PowerShell ISE”:

Step 2: Create and Save the PowerShell Script

Write the required code inside the “PowerShell ISE” console and click on the “Save” button:

>write-host "This is a script example"

Specify the file name, save it with the “.ps1” file extension, and hit the “Save” button:

Method 2: Creating a PowerShell Script Using Text Editor

Another method to create a PowerShell script is using a simple text editor. For instance, we will use the available resources in Windows to create a PowerShell script, such as “Notepad”. However, after creating a PowerShell script in Notepad, save it with the “.ps1” extension.

Step 1: Launch Notepad

First, launch “Notepad” from the Start menu:

Step 2: Write and Save Script

Write the required code and save the file by pressing the “CTRL+S” shortcut:

Similarly, enter the PowerShell script file name. Make sure to write the “.ps1” file extension at the end of the file name and hit the “Save” button.

Method 3: Creating a PowerShell Script Using Third-party IDE (Visual Studio)

Third-party software can also be utilized to create a PowerShell script, such as “Visual Studio” code editor. Visual Studio is a cross-platform source code editor used to create code or scripts for various programming languages.

For creating a PowerShell script using Third-party IDE, check out the given procedure.

Step 1: Launch Visual Studio

First, navigate to the Start menu and launch “Visual Studio”:

Step 2: Create and Save Script

Write out the required cmdlets and press the “CTRL+S” shortcut to save the file:

Likewise, save the file by specifying its name and its extension as “.ps1”.

Conclusion

To create a PowerShell script, first, launch “PowerShell ISE”, write script or code inside it and save the script by clicking on the “Save” button. There are also other tools to create a PowerShell script, such as Notepad, and Visual Studio code editor. This tutorial has presented several methods to create or write PowerShell scripts.

About the author

Muhammad Farhan

I am a Computer Science graduate and now a technical writer who loves to provide the easiest solutions to the most difficult problems related to Windows, Linux, and Web designing. My love for Computer Science emerges every day because of its ease in our everyday life.