Learning PowerShell — Let’s Do It Together — Part 3

Kamran Saifullah
4 min readFeb 9, 2020
https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_variables?view=powershell-7, https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_operators?view=powershell-7

So far we have discussed the cmdlets. Now we will take a look onto variables and then we will discuss different operators which can be performed.

Variables

Variables are storage spaces which when defined the OS allocates them space. We use variables to store our data. In PowerShell defining variable is different as different programming languages as well as scripting languages define variables using their own conventions.

Variables in PowerShell start with a Dollar Sign ($). Examples are as above.

$var1

$var_String

$varInteger

$1var

Let’s work with different operators. At first we will take a look onto assignment operator (=). This operator is used to define values into variables.

We defined a variable and then added an integer. We can output the variable values by directly calling the variables. Let’s add characters into a variable. We do this similar to other languages i.e. by putting the characters in single quotes or double quotes.

Let’s add float values into the variable.

Let’s add strings into the variable.

Operators

Now we have a good understanding of the variables. Now let’s take a look onto different operators available in PowerShell.

Arithmetic Operators

There are common arithmetic operators which are supported by PowerShell. Take a look onto the above screenshot!

Assignment Operators

There are common assignment operators which are supported by PowerShell. Take a look onto the above screenshot.

Comparison Operators

Comparison operators definitions are different from the ones in other programming languages although their working is same as of others.

-eq → Equals To
-ne → Not Euals To
-gt → Greater Than
-lt → Less Than
-ge → Greater Than or Equals To
-le → Less Than or Equals To

Example is as above!

Comparison Operators

These operators are used to find and replace patterns in text. These include the following. These operators use regular expressions.

-match → To match a particular value
-notmatch → To check if there is no match of the value
-replace → To replace a particular value

Example is as above!

-match → Matches if a string exists in the data.
-notmatch → Checks if a particular string does not exists.
-replace → Replaces a particular string with the string provided.

Containment Operators

These operators determine whether a test value appears in a reference set or not.

Example is as above!

-in → Checks if a particular value exists in the array or in set of values.
-notin → Opposite of -in
-notcontains → Checks if a value exists in the set or not!

Logical Operators

These operators are used to check for the conditions and also are used to write complex statements with ease. There are a number of logical operators in PowerShell.

All details regarding PowerShell operators can be found on the above link!

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_operators?view=powershell-7

--

--

Kamran Saifullah

Malware/RE/Firmware Analysis, App Sec/Off Sec, VAPT, Phishing Simulations/SE | Risk Management, IS Governance, Audits, ISO 27001 LI