WordsOnTech

PowerShell – Basics Concepts and Variables

In this series, we will learn basics of PowerShell. This Blog will focus on some of the basic terminology and how we can work with Variables in PowerShell.

Introduction to PowerShell

PowerShell was introduced with Windows 2008/R2. Since then, many versions of PowerShell have launched and with every new version, new features have been introduced.

One of the major changes came with version 5.1, when SQL module was introduced and that proved to be a boon for all SQL Server DBAs. With Version 6.X, PowerShell became compatible with Linux.

Advantages of PowerShell

PowerShell a great tool to work with. Below are some of the advantages of using PowerShell:

Concepts

PowerShell vs PowerShell ISE

As per official words used by Microsoft, “PowerShell is a simpler and more straightforward scripting and execution environment, while the ISE provides more flexible and forgiving editing and execution features.”

PowerShell ISE, in more simple terms, provides options for searching, has auto suggestion feature, which helps new users immensly.

Pipeline “|” in PowerShell

Official definition is “A pipeline is a series of commands connected by pipeline operators (|) (ASCII 124). Each pipeline operator sends the results of the preceding command to the next command. The output of the first command can be sent for processing as input to the second command. And that output can be sent to yet another command.

In simple terms, it helps in sending output of one command as an input to another. We will learn more about Pipeline in later sections.

Top-Down approach

PowerShell uses a top-down approach in execution. This means that, the command at top of the script will be executed first followed by command written below in sequential order.

Object Oriented

PowerShell treats everything as objects, which have their own attributes and properties. One can use these attributes as per his advantages. We will learn more on this, when we will go through cmdlets.

File extensions

A script is a plain text file that contains one or more PowerShell commands. PowerShell scripts have a .ps1 file extension.

Variables

There are three kinds of variables in PowerShell. Let’s look at them one by one.

Environment Variables

Environment variables store data that’s used by the operating system and other programs. They start with $Env:<variable-name>.

Some of the examples of these are:
$env:windir #This shown the folder where windows is installed. For me, it is C:\Windows
$env:HOMEPATH #this shows the user path from which command is being run. For example for me it’s Users\wordsontech

PowerShell ISE showing various variables that can be used with $env:

Automatic or Intrinsic Variables

These are pre-defined variables in PowerShell. Some of the examples of these variables ares:

$Psversiontable #shows the version of the PowerShell being used.
$host #shows from which host command is being invoked. For e.g., PowerShell ISE and it also contains version details of PowerShell.
$profile #Shows the folder location in windows from where command is being run.
$home #it shows the user information running the command.
$ErrorActionpreference #shows what is the preference set if an error is encountered. It can be modified if required.

Here is the Microsoft link explaining all of the automatic variables.

User Variables

Like any other language, we can define variables that can be used in scripts. To define a variable, “$” sign is used. For e.g., $name, $address, $age etc.

If you noticed, we don’t have to define the type of variable in the declaration. Based on the value, the variable is automatically treated as a string or an integer or any other datatype.

Let’s see some examples.:

$age = 10 #(becomes an Int32)
$address = "India" #(becomes a string)
$age = "ten" # (becomes a string and overwrites the Int32)

#Let's play with Variables
$age = 10
$address = "India"
$age2 = "12"

#What will be the outcome of below additions?

$age + $address 
<#
this will fail stating cannot convert string to an integer. PowerShell tries to convert second variable's data type to first variable's data type. Here first variable is integer and hence it tries to convert $address to integer which throws the error.
#>
$address + $age
<#
output will be India10 as it converts integer to a string which is possible and then does a concatenation.
#>
$age+ $age2
<#
output will be 22. this will be an addition as PowerShell converts string "12" to an integer 12 and then adds them up.
#>
$age2 + $age
<#
Output will be 1210. This happens as PowerShell changes integer 10 to string "10" and then concatenates with $age2.
#>

I hope you get the gist of basics that we have discussed here. Please ask any questions that comes to your mind.

Happy learning!

Exit mobile version