Getting Started With PowerShell
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed what is PowerShell and how to install PowerShell.
https://cloudaffaire.com/how-to-install-powershell-in-linux/
In this blog post, we will discuss how to get started with PowerShell. PowerShell is one of the most popular and powerful scripting languages to automate mostly Windows-based workloads. Though the new version of PowerShell also supports some level of capability for Linux and other OS workload automation. PowerShell is also supported in all major Cloud platform to automate cloud infrastructure.
What makes PowerShell stand-up to other scripting languages is the ease of use, in-depth documentation, and the most importantly its ability to process objects. Though PowerShell is mainly used as a scripting language it fully supports concepts of OOP (Object Oriented Programming). Every command that you execute in PowerShell returns the output as an object instead of plain text and the same can be passed to other commands using the pipeline. This makes processing and extraction of data from input and output very easy and simple.
Getting Started With PowerShell:
Launch A PowerShell Session:
You can directly launch a new PowerShell session from the windows start menu. Or run “powershell” from run or windows command prompt to launch a new PowerShell session.
You can also launch the PowerShell session in administrative mode by selecting right-clicking and selecting “Run as administrator”.
In Linux OS to launch a new PowerShell session, type “pwsh” in the terminal.
Comment In PowerShell:
A comment is a code block that does not gets executed or ignored during the runtime. A comment is a way to provide additional details to your script. In PowerShell, you can use # (hash) on the beginning of the line to us as a comment. For a multi-line comment, you can use <# to start the comment and #> to end the comment.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
################################## ## PowerShell | Getting Started ## ################################## ## PowerShell Version 5 or more ## -------- ## Comments ## -------- # single line comment <# multi line comment #> |
Input-Output In PowerShell:
There are multiple ways to output the data in PowerShell terminal. You can use cmdlet like Write-Host or Write-Output to write the output in the terminal. You can use Read-Host cmdlet to take input from the users.
1 2 3 4 5 6 7 8 9 10 11 12 |
## ------------ ## Input Output ## ------------ ## print output Write-Host "Hello World"; ## returns Hello World Write-Output "Hello World"; ## returns Hello World ## take input $name = Read-Host "enter your name"; ## prompts "enter your name" and assigned the value to variable $name $name; ## returns input of the above command Write-Host "Hello $name"; ## returns "Hello" and input of the above command |
Variables In PowerShell:
The variable in a nutshell is a temporary space to store some value. In PowerShell, you can store different data types as a variable value. Variables can be user-defined or system-defined and the scope of the variables can be local or global. A variable is defined as $<variable_name> = <variable_value> and variable value can be retrieved using $<variable_name>. PowerShell automatically assigns the datatype of the variable depending upon the value assigned to a variable.
1 2 3 4 5 6 7 8 9 |
## --------- ## Variables ## --------- $a = 4 $b = "Debjeet" Write-Host $a ## returns 4 Write-Host $b ## returns "Debjeet" |
Conditions In PowerShell:
You can use conditions in PowerShell script for decision making. In PowerShell, we mainly use the IF conditional statement for decision making. PowerShell supports if, if-else, and if elseif else conditional statements. Apart from if, the switch can also be used as a conditional statement.
1 2 3 4 5 6 7 8 9 10 11 |
## ---------- ## Conditions ## ---------- $a = 10 $b = 5 if ($a -gt -$b) { Write-Host "$a is greater than $b" ## returns "10 is greater than 5" } |
Loops In PowerShell:
A loop in a nutshell is a way to repeat a command or statement until a certain condition is met. PowerShell supports for, foreach, while, do-while, and do-until loops.
1 2 3 4 5 6 7 8 |
## ---- ## Loop ## ---- for ($i = 1; $i -le 5; $i++) { Write-Host $i ## returns 1,2,3,4,5 } |
Array And Hash Table In PowerShell:
An array is a data structure consisting of a collection of elements (values or variables), each identified by at least one array index or key. An array is stored such that the position of each element can be computed from its index tuple. The simplest type of data structure is a linear array, also called one-dimensional array. A hash table, also known as a dictionary or associative array, is a compact data structure that stores one or more key/value pairs.
1 2 3 4 5 6 7 8 9 |
## --------------------- ## Array And Hash Tables ## --------------------- $a = @(1,2,3,4,5) $h = @{one = 1; two = 2} $a $h |
Functions In PowerShell:
A function is in a nutshell grouping of multiple commands together that can be reused. The main benefit if the function is its reusability, you have to write the function only once and then you can reuse it as many times as you wish.
1 2 3 4 5 6 7 8 9 |
## --------- ## Functions ## --------- function myfun { Write-Host "Hello World" } myfun |
Get PowerShell Version:
PowerShell version details are stored in system variable $PSVersionTable.
1 2 3 4 5 |
## ------------------ ## PowerShell version ## ------------------ $PSVersionTable ## get powershell version details |
Cmdlet In PowerShell:
A cmdlet is a lightweight command that is used in the PowerShell environment. The PowerShell runtime invokes these cmdlets within the context of automation scripts that are provided at the command line. The PowerShell runtime also invokes them programmatically through PowerShell APIs.
1 2 3 4 5 6 7 8 9 10 11 12 |
## ------ ## cmdlet ## ------ ## including cmdlets, aliases, functions, filters, scripts, and applications Get-Command ## get help on a cmdlet Get-Help Get-Process ## execute a cmdlet Get-Process |
Pipeline In PowerShell:
Pipelines are arguably the most valuable concept used in command-line interfaces. When used properly, pipelines reduce the effort of using complex commands and make it easier to see the flow of work. Each command in a pipeline passes its output, item by item, to the next command. Commands don’t have to handle more than one item at a time. Pipelines are represented by | symbol.
1 2 3 4 5 6 |
## -------- ## Pipeline ## -------- ## get all cmdlet Get-Command | Where-Object {$_.CommandType -eq "Cmdlet"} |
Execute A Script In PowerShell:
Instead of running commands or cmdlets in the terminal, you can create a script file that contains all your PowerShell script or command and then execute the script in PowerShell.
1 2 3 4 5 6 7 8 9 |
## ------------------------------ ## Execute A Script In PowerShell ## ------------------------------ Write-Output 'Write-Host "Hello World"' > myscript.ps1 .\myscript.ps1 Remove-Item .\myscript.ps1 |
Getting Help In PowerShell:
You can use Get-Help cmdlet to get help on specific cmdlets.
1 2 3 4 5 6 7 8 9 10 11 |
## ------------ ## Getting Help ## ------------ Get-Help Get-Process Get-Help Get-Process -Detailed Get-Help Get-Process -Examples Get-Help Get-Process -Online |
Hope you have enjoyed this article. We will cover each of these topics in the coming blog posts. In the next blog post, we will discuss Variables In PowerShell.
To get more details on PowerShell, kindly follow below official documentation