Introduction
PowerShell is an object-oriented automation engine and scripting language with an interactive command-line shell. Microsoft designed PowerShell to automate system tasks, such as batch processing.
Why we use powerShell?
The most appealing reason to use any kind of CLI is the potential for precise and repeatable control over a desired action or task flow that is difficult, or even impossible, to replicate with a traditional GUI.GUIs were designed to be comfortable for humans to use, but they can be time-consuming, cumbersome and error-prone — especially when a task must be repeated hundreds or thousands of times.
Installation of PowerShell
The installation of PowerShell depends on the type of operations system we would like to run the PowerShell Mac, Lunix, or Windows. Microsoft Windows has a PowerShell by default. The installation of PowerShell for Mac can be made in several manner, but the most simple approach is using HomeBrew package manager. if the HomeBrew is not installed, make sure to install HomeBrew as follow
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Figure 1.1 : – Installing HomeBrew
Installation of PowerShell then performed as follow using HomeBrew
brew install powershell/tap/powershell
Figure 1.2 : – Installing Powershell
Checking the installation of PowerShell
pwsh
PowerShell 7.3.8
ps/Users/userName
$PSVersionTable
Name Value
---- -----
PSVersion 7.3.8
PSEdition Core
GitCommitId 7.3.8
OS Darwin 20.6.0 Darwin Kernel Version 20.6.0: Wed Jan 12 22:22:45 P…
Platform Unix
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
Variables
It is easier to declare a variable in PowerShell simply using a $ sign along with the name of the variable, and the value to assign through the assignment operator. the syntax for declaring and, initialization
$age=3
Write-host $age // Will show the value assigned to a variable
Control Flow
Control flow is a common statement that is used in any problem solving in programming language. some of the known control flows are If statements, switch and many other. Let us consider the if conditional statement
Switch Statement that is branching to the value that is set in the computation ignorer to execute the correct part of the code
$options = 3
switch($options){
0
{
Write-host "It is Zero" -foregroundcolor Green
}
1
{
Write-host "It is One" -foregroundcolor Green
}
Default
{
Write-host "It is Default" -foregroundcolor Green
}
}
OUTCOME
It is Default