in

WindowsWindows Loving ItLoving It

PowerShell Parentheses, Box Brackets, Braces, Variables and more

PowerShell Parentheses are used for condition blocks, expressions, and much more. We use Box Bracket, Curly Braces, Variables, Comments with examples

PowerShell Parentheses

Today we are going to talk about PowerShell punctuations.  This includes the parentheses, curly brackets, box bracket and more.  These punctuations are the first hurdle between a new PowerShell learner and a smooth learning process.  In this post, we will cross this hurdle with examples and samples and ensure that anyone can carry forward with their PowerShell learnings.

Jump to:

People also search for:

PowerShell is a command-line shell and also a scripting language.  It is needed for IT professionals and System Admins.  It can be difficult at the start but with enough effort, it can be very useful and productive.

Without further delays, let us start with the Parentheses.

PowerShell Parentheses:

When it comes to brackets, parentheses are the most important.  That is because it is given the highest importance in the expression and also it is used to differentiate between a variable, function call, and syntax.

Here we will look at an example that uses parentheses for expression and syntax.

$a = 1..10

foreach($i in $a)
{
if(($i+5)/5 -eq 2)
{$i}
}

PowerShell parentheses
PowerShell parentheses

In the above script, we can see that the parentheses are used for declaring a loop.  The parentheses enclose the necessary variables of the loop.

foreach($i in $a)

This part is used to declare $i which keeps the corresponding element in the array in the respective iteration. And thus parentheses are used for syntax purposes also.

In the condition statement, we want to evaluate $i+10 first and then divide the outcome with 5. If we run the script we get 5 as the output.

If we run the code without the parentheses, we will get the following output.

foreach($i in $a)
{
if($i+5/5 -eq 2)
{$i}
}

without PowerShell parentheses
without PowerShell parentheses

Clearly we need the parentheses to explain to PowerShell which expression to evaluate first and which one to evaluate next.  And we need parentheses for telling PowerShell how some parts of the scripts belong to the condition block.

The last use case of parentheses is a function call.  Consider the following script.

$array = @(‘hello’,’how’,’are’,’you?’)

$array.contains(‘hello’)

function call
function call

The above script will return true as shown in the image.  Here we are using parentheses to call the function and pass the parameter to it. We need to note that we are passing the parameter to the .NET function.  PowerShell function does not need parentheses while passing parameters to it.

PowerShell Curly Braces:

From the earlier example, we have already seen the use of the Curly brace.

foreach($i in $a)
{
if($i+5/5 -eq 2)
{$i}
}

without PowerShell parentheses
without PowerShell parentheses

In the above script, only the if condition statement is enclosed inside the for-loop block and only one line is enclosed inside the if condition block.  That means the script inside the loop block will be executed over and over again while only the block inside the condition will be executed when the condition is true.

In PowerShell, we are using curly braces to differentiate between different logical blocks of scripts.  Some scripts belong together and they are enclosed in curly braces.

PowerShell Box Brackets:

The box brackets are used for arrays.  We can create an array and use the box bracket to get to an element at a specific position.

The below scripts shows just this.

$array = @(‘hello’,’how’,’are’,’you?’)

$array[3]

array
array

The above script will give out ‘you?’ as the output because it is the element in index 3.

We can take note that the box bracket can be used while passing optional parameters also.

gps [w]*

The output of the above script is given below.

box brackets
box brackets

Here the box bracket is used to tell that we are looking for outputs that start with ‘w’.  Note that we need to add ‘*’ otherwise we will not get the desired output.

PowerShell Comments:

Single line comments are started with the has a tag or the octothorp.

#This is one line comment

The multi-line comment is enclosed between <# and #>

<# this is
multi line
comment #>

The output is shown in the image.

PowerShell comments
PowerShell comments

PowerShell Variables:

The variables names in PowerShell starts with the dollar sign $.  We can use the alphabets, numbers, underscore and question marks.  We can also wrap the entire name with ${} and use it as a variable name.

${what is going on} = 5

The above given is a valid variable name.

variable names
variable names

If the variable is an array, we need to use the box bracket to pass the index of the position we want.  And if the variable is an object, we can use the dot operator to access the properties that are inside the object.

PowerShell quotes:

The quotes in PowerShell are mainly used for passing a string to a variable.  There are different variations to using the quotes, we should use double quotes if we want to interpolate strings as we do below.

$num = 5

“I have $num apples”

The output is given in the image.

double quotes
double quotes

We have to use the single quote to create a string.

use single quote
use single quote

The same script with double quotes replaced with a single quote gives us a different output.

Verdict:

In this post, we learned about the different brackets and their main use cases in Powershell.  The brackets are used for a function call, script blocks, condition blocks, expressions and much more.  Different types of brackets are needed for different scripts.  Along with that, we learned about the different syntax for variables, comments and more.

If you want more awesome posts on PowerShell, stay tuned to TecKangaroo.

Leave a Reply

Your email address will not be published. Required fields are marked *