You are currently viewing Getting Started With PowerShell

Getting Started With PowerShell

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.

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.

Getting Started With PowerShell

You can also launch the PowerShell session in administrative mode by selecting right-clicking and selecting “Run as administrator”.

Getting Started With PowerShell

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.

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.

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.

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.

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.

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.

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.

Get PowerShell Version:

PowerShell version details are stored in system variable $PSVersionTable.

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.

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.

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.

Getting Help In PowerShell:

You can use Get-Help cmdlet to get help on specific cmdlets.

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


Leave a Reply