Windows PowerShell Brackets

Introduction to Windows PowerShell - Type of Bracket

Did you ever buy a light bulb with a bayonet socket, only to discover that you really needed a bulb with a screw fitting?  Well, PowerShell brackets require similar attention to detail otherwise your script will also remain in the dark.

Windows PowerShell employs four types of bracket, (parenthesis, also called curved) {braces, sometimes called curly} and [square].  Occasionally you may also see <angle> brackets.  My point is that when you use PowerShell commands, each type of bracket has a particular meaning and significance.

Types of PowerShell Bracket

  1. Parenthesis Bracket (Curvy)
  2. Braces Bracket {Curly}
  3. Square Bracket []
  4. Summary of PowerShell Brackets


1) Parenthesis Bracket ()

When a PowerShell construction requires multiple sets of brackets, the parenthesis (style) usually comes first.  Parenthesis brackets also come first in importance because these (curved) brackets are used for what Guy calls compulsory arguments.  Experts call these control structures.

Let us take a foreach loop as an example.  The (input definition) is the most important element; it comes first and is enclosed by parenthesis.  Observe that the {braces} style of bracket, comes second and inside these braces is a {Statement Block}, which dictates the code to execute.

Another way of looking at parenthesis is they resolve problems of ambiguity; if PowerShell cannot decide which order to process components, clarify your instructions with parentheses.

Example 1: To Demonstrate (parenthesis) and {braces} Brackets

While this example lists your network adapters, my purpose is introduce the brackets in the control structure.

# PowerShell cmdlet to demonstrate brackets
$WmiClass = "Win32_NetworkAdapterConfiguration"
$colItems = Get-WmiObject -class $WmiClass
ForEach ($objItem in $colItems)
    Write-Host $objItem.caption
    Write-Host $objItem.ipaddress
    Write-Host $objItem.macAddress

Note 1: Observe how the parenthesis brackets enclose the compulsory statement, or control element ($objItem in $colItems), which starts the foreach loop.

Note 2: Find the statement block, enclosed by {braces}.  This clause is important because it determines what to do inside each loop.  Where the statement block contains multiple lines, each half of the bracket has its own separate line, thus emphasising the action section of the script.

Note 3: While arrays are not covered here, please note that the parentheses style of bracket would be used to declare an array.  For example, @() always return arrays.

Guy Recommends: Free WMI Monitor for PowerShellSolarwinds Free WMI Monitor for PowerShell

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft's operating systems.  Fortunately, SolarWinds have created a Free WMI Monitor so that you can discover these gems of performance information, and thus improve your PowerShell scripts.

Take the guess work out of which WMI counters to use when scripting the operating system, Active Directory, or Exchange Server. Give this WMI monitor a try - it's free.

Download your free copy of WMI Monitor

Example 2: PowerShell Braces Bracket { }

Employ braces, or curly brackets whenever you want to store a statement block within your script.  A common place to pay attention to the braces style of PowerShell bracket is when you initiate a 'Where' clause.

# PowerShell curly bracket example
Get-WmiObject -List | Where-Object {$ -Match "win32*"}

Note 4: Remember to the use of the | pipeline, because this takes the output of 'Get-WmiObject -List', and passes it as input to the 'where' clause.

PowerShell 'If' Statement - Braces Bracket {}

Remember that with 'If' the (parenthesis are for the condition), whereas {braces are for the block command}.

If (condition) {block command}
# Another explanation would be
If (test) {
"Execute payload when test is true"

See More on PowerShell's If Statement »

Guy Recommends:  A Free Trial of the Network Performance Monitor (NPM)Review of Orion NPM v11.5 v11.5

SolarWinds' Network Performance Monitor will help you discover what's happening on your network.  This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.

What I like best is the way NPM suggests solutions to network problems.  Its also has the ability to monitor the health of individual VMware virtual machines.  If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.

Download a free trial of Solarwinds' Network Performance Monitor

 Example 3: PowerShell's Square Bracket [ ]

Guy thinks that the word which best sums up PowerShell's square bracket is: optional.  Anytime you want to tag on an optional command or parameter, select the [square] bracket.

However, experts think of square brackets as providing access to arrays.  To the expert, they introduce type names [2], and as character classes for regular expressions.  Another instance of the square bracket is for the simpler 'like' operator, which is used for filename globbing.

Example 3a
This is one of the most useful application of PowerShell's square bracket.  The purpose is to filter out those properties beginning with __

# Example of PowerShell Square Bracket
Get-WmiObject Win32_Computersystem | Get-Member -Membertype property [a-z]*

Example 3b
List all the processes beginning with the letter 's'

Get-Process [s]*

Example 3c
Wildcards in square bracket can produce unexpected results.  It's just a matter of trial and error and also you need to adjust to PowerShell's logic; [s-t] means beginning with 's', or beginning 't'.   '[SVC]' means beginning with 'S' or 'V' or 'C' and not beginning with specifically 'SVC....'.

Get-Process [r-s]*

Note 5: Experiment with different letter combinations, thus become expert at using the hyphen-filter.

Note 6: Pay attention to the wildcard asterisk*.  See what happens if you omit the *.  Try:
Get-Process [r*-s], or Get-Process [r-s]*.  Did they produce what you expected?  I was disappointed with [r*-s], but on reflection perhaps it was a foolish request.

Conclusion About PowerShell's Brackets

At first sight, one type of bracket seems much like another, but as you gain experience with a variety of PowerShell constructions, you begin to tune-in to the differences.  Eventually, you reach a level of expertise where it seems that the very type of PowerShell bracket is trying to tell you something of significance.  The bottom line is that if we employ the wrong type of bracket, instead of executing our commands, PowerShell presents us with an error message.

See More on PowerShell's Syntax »

Summary of PowerShell's Brackets

In a nutshell, the type of bracket is highly significant in Windows PowerShell. Take the time to tune-In to the personality of each style of bracket.

Parenthesis () come first, both in sequence and in importance. Braces {} play a specific role in 'Where' statements and also anywhere you need to employ a statement block. Lastly, Guy thinks of PowerShell's square [] brackets as controlling optional parameters.  Experts think of square brackets as providing access to an array.

The most important lesson is that each type of PowerShell bracket has a particular role; you must choose the correct bracket for the particular scripting task.

Guy would like to acknowledge Luke Breuer's help in providing deeper insights into PowerShell's brackets.

If you like this page then please share it with your friends


See more Windows PowerShell flow control examples

PowerShell Home  • PowerShell If Statement  • PowerShell ElseIf   • Free Permissions Analyzer

Conditional Operators  • PowerShell -Match  • PowerShell -Like  • PowerShell -Contains

PowerShell Comparison Operators  • PowerShell Syntax   • Where Filter  • PowerShell Else

Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.



Custom Search

Site Home

Guy Recommends: WMI Monitor for PowershellSolarwinds WMI Monitor

Windows Management Instrumentation (WMI) is most useful for PowerShell scripting.

SolarWinds have produced this Free WMI Monitor to take the guess work out of which WMI counters to use for applications like Microsoft Active Directory, SQL or Exchange Server.

Download your free copy of WMI Monitor

Author: Guy Thomas Copyright © 1999-2016 Computer Performance LTD All rights reserved.

Please report a broken link, or an error to: