PowerShell is a command-line tool created by Microsoft to run commands and scripts to modify settings and automate activities in windows OS. It offers a broader range of tools, flexibility, and control. It is a .Net-based programming language that enables you to fix problems quickly by assisting system administrators in reducing future manual labor hours. It is primarily intended for IT professionals and system administrators to handle and automate Windows OS and other program administration. It introduces intriguing new concepts that build on the skills you’ve developed and the scripts you’ve written in the Windows Command Prompt and Windows Script Host environments. This article will go through all the key points you need to know to understand PowerShell.

PowerShell Integrated Scripting Environment (ISE):

Two PowerShell programs are available in windows 10, namely PowerShell and the PowerShell Integrated scripting environment (ISE). You can use either of them, but the PowerShell ISE is the recommended application. The reason is that it includes syntax highlighting, auto-filling of commands, and other automation capabilities that ease scriptwriting and testing in a GUI-based environment. It also consists of a comprehensive list of common modules and command let (cmdlets) functions system administrators might use. In PowerShell (ISE), there are two different windows: the scripting window where you will write all your scripts, and the other is the console window where you can call or run the script and see its output. When you’re ready to start developing your cmdlet functions, you may use the console window to test your code, find flaws or difficulties, and then work to repair them, as shown below.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/image1-22.png" data-lazy- height="566" src="data:image/svg xml,” width=”582″>

The PowerShell ISE, like other development environments, is highly configurable. Users may pick the color scheme, typeface, and theme they want to use when developing scripts. Also, it provides the builtin commands that can be used while writing a command so that you don’t need to memorize them, as shown below:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/image3-21.png" data-lazy- height="736" src="data:image/svg xml,” width=”762″>

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/image2-22.png" data-lazy- height="966" src="data:image/svg xml,” width=”1174″>

The easiest way to start working on it is to write PowerShell ISE in the Start menu search bar, as shown in the image below.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/image5-17.png" data-lazy- height="851" src="data:image/svg xml,” width=”1026″>

How to write the first program in Powershell:

The sole limitation is that the default security protocol permanently blocks any script executing on a device. This means that if you double-click a “.ps1” file on Windows 10, nothing happens, and if you try to run the script in PowerShell, you’ll get an error message saying, “Cannot be loaded because executing scripts is disabled on this system.” So you can check the current execution policy status by the following command shown below.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/image4-18.png" data-lazy- height="433" src="data:image/svg xml,” width=”717″>

Here you can see that the default status of the execution policy is set to “Restricted,” which means that you can’t run any script. The following command indicates how you can modify the state of the execution policy.

> Set-ExecutionPolicy RemoteSigned

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/image7-16.png" data-lazy- height="444" src="data:image/svg xml,” width=”723″>

As you can see in the above image, the execution policy status is now changed to “RemoteSigned.” Now, you are ready to write your programs and run the script later. As a first program, we will try something easy, as shown below.

Write-Host “This is my first program.”

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/image6-18.png" data-lazy- height="494" src="data:image/svg xml,” width=”717″>

This script will display a message “This is my first program” upon execution. After that, you need to save your program with the extension of “.ps1” and place it in the desired folder. For example, the above program is saved with a file name of “FirstProg” with an extension of “.ps1,” and its place in a folder with the name of “Shell.” Make sure you are in the same directory where the script is placed. You need to call your script in the console window and write the following command to run this program.

How to create a folder and file in PowerShell:

Suppose you are trying to create a folder with the name “Power Shell” in the C directory, then you can create by using the following command:

> New-Item -Path ‘C:PowerShell’ -ItemType Directory

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/image9-12.png" data-lazy- height="716" src="data:image/svg xml,” width=”1188″>

The output of this command will be “Directory: C:,” which shows that the folder has been created. Also, you can create a subfolder inside a “Power Shell” folder by following the same format.

> New-Item -Path ‘C:Power ShellTestFolder’ -ItemType Directory

Also, you can create a file with a filename of Power Shell using the following command:

> New-Item -Path ‘C:Test.txt’ -ItemType File

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/image8-13.png" data-lazy- height="742" src="data:image/svg xml,” width=”980″>

This command’s output will be the same as shown above, but this time you can see the text file with the name of “Test” when you open the C drive. Similarly, you can create any text file inside a folder as shown below:

> New-Item -Path ‘C:Power ShellTest.txt’ -ItemType File

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/image12-8.png" data-lazy- height="730" src="data:image/svg xml,” width=”929″>

As you can see from the above image, a file has been created with the name of “Test.txt” placed in the folder of “Power Shell.

How to assign the variables in PowerShell:

So, before we discuss PowerShell programming, it’s better to understand how to initialize the variables. You can initialize them by writing the ‘$‘ sign first and then whatever name you like for your variables. For example, $var1, $var2, $a, $b etc.

How operators work in PowerShell:

PowerShell provides a large variety of different operators that you can utilize to get the desired result. Some most used operators are as follows:

  • Arithmetic Operator
  • Assignment Operator
  • Comparison Operator 
  • Logical Operator

Now we will discuss some examples of these operators for a better understanding of the syntax and functionality.

Arithmetic Operator

This operator is used for the addition, subtraction, multiplication, and division of variables. Its examples, along with its syntax, is shown below:

Operators Syntax
Addition $Var1 $Var2
Subtraction $Var1 – $Var2
Multiplication $Var1 * $Var2
Division $Var1 / $Var2

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/image11-8.png" data-lazy- height="470" src="data:image/svg xml,” width=”916″>

$Var1 = 10

$var2 = 5

$var1 $var2  #Addition

$Var1$var2  #Subtraction

$Var1 * $var2  #Multiplication

$Var1 -lt $var2  #Division

Assignment Operators:

Assignment operators are based on some conditions, and if that condition satisfies, it will give you the output as “True”; if not, it will give “False.”

Operators Syntax
Equals $Var1 -eq $Var2
Not Equals $Var1 -ne $Var2
Greater Than $Var1 -gt $Var2
Less Than $Var1 -lt $Var2

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/image10-11.png" data-lazy- height="605" src="data:image/svg xml,” width=”869″>

$Var1 = 10$var2 = 5

$var1 -eq $var2  #equality

$Var1 -ne $var2  #non equlaity

$Var1 -gt $var2  #greater than

$Var1 -lt $var2  #less than

How conditional statement works in PowerShell:

These rules will execute when a specific condition has been met that can either be true or false. So of the examples are If, If-else, switch, while conditions, but in this article, we will discuss only If and If-else conditions as shown in the images below.

If condition example:

$Var1 = 10

if ($a -le 20) #If condition testing

{Write-host (” This variable is less than 20″)}

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/image13-5.png" data-lazy- height="464" src="data:image/svg xml,” width=”871″>

If else condition example:

$a = 10 #If else condition testing

if ($a -le 5)

{Write-host (” This variable is less than 5″)}

else { write-host (” This variable is greater than 5″)}

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/image14-4.png" data-lazy- height="408" src="data:image/svg xml,” width=”899″>

How Loop structure works in PowerShell:

Loops are used to repeat any process multiple times until the condition is met. Loops are used to simplify your scripts. Let’s discuss “For Loop” here, which is the most commonly used condition. We will take an example to count numbers from 1 to 10, which can be seen below.

for ($num =1; $num -le 10; $num ) #counting from 1 to 10

{$num}

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/image15-3.png" data-lazy- height="640" src="data:image/svg xml,” width=”1083″>

Conclusion:

We hope this beginner’s PowerShell scripting guide has provided you with all you need to get started with PowerShell scripts. Working with scripts is simple after you’ve learned the basics of PowerShell syntax: make sure you keep all of your scripts structured and name them in a way that makes it clear what they do. That way, you won’t be perplexed. Once you’ve learned to script, there’s no limit to what PowerShell can accomplish for you.