Powershell

PowerShell Operators

There are a lot of operators found in the field of Computers, mostly in programming for computation and manipulation of different integers, strings, and variables. Those operators can be the arithmetic, logical, comparison, assignment, and many more. Today, we will be looking at all those types of Operators with some examples in PowerShell of Ubuntu 20.04 system. So, make sure you have PowerShell already installed and configured within the shell terminal of Ubuntu 20.04. Open the shell terminal with the Ctrl+Alt+T shortcut. You have to start the Powershell using the “pwsh” command in the shell, as shown below.

Arithmetic Operators:

These are used for the calculation of mathematical equations or expressions. So, we will be having examples of all the possible arithmetic operators here. Let’s start by using the addition (+) operator first. So, we have simply placed the plus sign as an operator between two integer operands. As a result, we have got 4 as the sum of both integers.

You can also apply the arithmetic operators to variable values. So, we have initialized two integer variables, used the plus operator to calculate their sum, and saved them to another variable. The resultant variable value has been displayed using the “echo” statement, i.e., 11.

Let’s use the addition operator between string values as per the below variables. We have got the concatenated value in return.

Subtraction Operator

The subtraction operator is used to subtract one value from another. It gives errors upon application to the string values. Firstly, we have subtracted the greater value from a small value and got a negative result. On the other line, we have got the positive integer as the greater one is subtracting, the smaller value.

The same operator (-) has been applied to two integer variables. The first calculation gives the negative result, and the second one gives the positive integer result.

Multiplication Operator

The multiplication operator can only work on integers and gives errors upon application on the string variables. So, we have got the multiplication result, i.e., 45, after using the “*” operator.

The same multiplication operator can be applied to variables, as shown in the image below.

Division Operator

This operator (/) can be utilized to divide two integer values. It can’t be applied to string values. So, the result of integer values is shown below.

The division operator works the same on the integer variables, as shown in the image beneath.

Modulus Operator

This operator provides the remainder of the modulo calculated with two integers. It returns “1” upon the calculation of modulus.

Assignment Operators:

These operators are used to allocate some values to other variables using the “=” sign. So, we have initialized two character variables. Both the character variables are concatenated with the “+” operator, then assigned the concatenated value to the new variable “v3”.

Without concatenation, you can also assign the value of one variable to another as below.

Another technique to allocate a value to a variable is with the “+=” sign jointly. You can see that the value of concatenation v1+v2, i.e., “v2+=v1” has been assigned to the variable “v2” and displayed on the shell as “BA”.

The same methods work similarly on the integer variables. From the below example, you can see that the variable v1 has a value of 5 and is summed up with “3” at the next step of the assignment. The result will be overloaded to the variable v1 again and displayed.

Negative Assignment Operator

The negative assignment operator also works the same but gives the smallest values in return. The variable v1 has a value of 9, and the assignment operator is decrementing it with 5. The resultant subtracted value will be again saved to the variable v1 and demonstrated on the screen.

Comparison Operators:

The comparison operators are utilized to compare two or more values to check which one is greater, less, or equal. Let’s take a look at equal operators, i.e., “eq” on integers, as shown below. The equal operator returns True upon matched otherwise False. So, it returns False as shown.

The equal operator can also be applied to the string values, as you see in the image. The first one returns True as both values are logically correct. The next line returns False due to capital and small letters.

The Not equal operator used between two integer values returns True as the condition met.

You can also use the Not Equal operator with a case sensitivity flag within the string values. You can see the output shows True in return as both values are not equal.

The > operator verifies if one value is larger than the second value. The below expression returns False as 4 is not greater than 7.

The >= operator is used below to see if values are equal and greater. Both returns True as 5 is greater than 4 and equal to 5.

The <= is used to check a value that is smaller than the second value. As you can comprehend from the screenshot below that 9 is less than 10, that’s why return True.

Logical Operators:

Logical operators return Boolean values in return, i.e., True or False. There are mainly three logical operators, i.e., NOT, AND, OR. These will be applied to more than 1 condition.

OR Operator

This operator returns True if one of its two conditions is True. It returns False if both the conditions are False, as shown below.

AND Operator

This operator returns True if both its conditions are True; else, it will return False.

NOT Operator

The NOT operator can reverse the result of any condition. Let’s say we have got False as applying AND on two expressions. Upon applying NOT, the False is reverted into True as below.

Upon applying the OR operator on two expressions, we have got True. This result has been reverted by False upon applying the NOT operator on it.

Conclusion:

We have discussed the PowerShell operators in this article guide. It covers all the arithmetic, assignment, logical, and comparison operators being applied on integers, strings, and characters. We believe it will be helpful to every naïve user.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.