PowerShell

PowerShell: Understanding Parentheses, Braces and Square Brackets

The goal for this blog post is to demystify the usage of PowerShell brackets for scripters and PowerShell enthusiasts. You can find braces everywhere, in scripts, in the PowerShell help and in simple one-liners. And there are three types. Let’s dive in.

() {} []

We distinguish three types of brackets:

  • Parentheses ()
  • Braces {}
  • Square Brackets []

Without a doubt, Parentheses are the most used bracket types in PowerShell. That’s why we will cover Parentheses first.

Parentheses ()

As already mentioned, () is the most seen bracket type in PowerShell. You will find it almost everywhere, for example in the PowerShell help examples which are a great source to learn more about PowerShell syntax.

1.PNG

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/get-date?view=powershell-6

The description tells us that the command is wrapped with parentheses so that the result is already there when the DayofYear property comes into play …

To make a long story short, we can say:

Objects (Commands, Variables, …) wrapped with parentheses are executed instantly.

For a better understanding I’ve prepared some examples.

Parentheses – Example 1

Take a look at the following one-liner. Note that the mathematical calculation (substraction) can only be carried out when the result of both statements is already there.


(Get-Date) - (Get-Date -Day 23 -Month 03 -Year 1976)

1.PNG

Parentheses – Example 2

The computernames retrieved by Get-Content wrapped with parentheses must be already there when Restart-Computer comes into play.

1.PNG

Parentheses – Example 3

When the script have to decide what to do next, the result of Test-Path in line 1 must already be there.

1.PNG

Which brings me to the next type of brackets: Braces.

Braces {}

Unlike Parentheses, objects in braces are not executed instantly. They are widely used in scriptblocks. As you can see below, doing some maths with commands in braces is not a good idea.

1.PNG

Objects (Commands, Variables, …) wrapped with braces are widely used in scriptblocks. They are executed when it is their turn.

Braces – Example 1

Let’s pick up our previous example again, in order to see the difference between () and {}.

1

Test-Path in line 1 is executed instantly. The script blocks from line 5 to 8 are executed when the condition is met, that means on the other hand that they are not executed when the condition is not met.

Braces – Example 2

The command in this example creates a scheduled job on the remote computer AzMember01. Pay attention to the braces.


Invoke-Command -ComputerName AzMember01 {Register-ScheduledJob -Name RestartAzMember01 -ScriptBlock {Restart-Computer -Force} -Trigger (New-JobTrigger -Once -At 05:15pm)}

unbenannt23.png

Several commands are wrapped with Braces. The statement is not executed instantly, because it makes no sense to do anything at first. The PowerShell engine must first examine the entire command to then be able to meaningfully decide what should be done.

Which brings me to the last part.

Square Brackets []

Square brackets retrieve elements in arrays or hashtables and serve as optional parameters.

Square Brackets – Example 1

For instance, let’s say we have an array.


$array=[array]('Peter','Margit','Patrick')

To retrieve array elements, use Square Brackets.

1.PNG

Square Brackets – Example 2

To get only services beginning with either letter r or s, you need to put r and s in Square Brackets.


Get-Service [rs]* | Select-Object Name,Status

Bild2.png

Last but not least an example that queries and displays all services which names ends with s or r.


Get-Service *[sr] | Select-Object Status,Name

Bild1.png

Square Brackets – Example 3

A hashtable is like an array, but it uses a key-value pair.


$hash=@{Kevin = '1'; Alex= '9'; Margit= '12'}

1.PNG

Now we access the key “Kevin” using Square Brackets and get the value 1.


$hash['Kevin']

1.PNG

I hope this article has helped to better understand the topic around brackets in PowerShell. All three bracket types were covered. I also hope that the examples are useful and that you can build on them.

See you next time with PowerShell!

Categories: PowerShell

Tagged as: ,

10 replies »

  1. No need for brackets when searching for services beginning with or finishing with a single character.
    This works fine:

    Get-Service r*

    Get-Service *s

    However, if you search for services beginning with either r or s, brackets are helpful:

    Get-Service [rs]*

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.