Guy recommends :
Free Solarwinds
VM Console

Solarwinds VM Console Free Download

Find out which of your VMs are a waste of space and which VMs need more resources.



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.

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.

Types of PowerShell Bracket

  1. Parenthesis Bracket ()
  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.

Example 1: To demonstrate (parenthesis) and {braces} brackets

# 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
    write-host
}

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: WMI Monitor and It's Free!Solarwinds Free WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft 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.

Download your free copy of WMI Monitor

2) 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 {$_.name -match "win32*"}

Note 1:  Remember to the use of the | pipeline, because this streams the output of 'Get-Wmiobject -list', and passes it as input to the 'where' clause.

3) 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
Clear-Host
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 1:  Experiment with different letter combinations, thus become expert at using the hyphen-filter.

Note 2:  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.

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 examples

PowerShell Home  • PowerShell If Statement  • PowerShell ElseIf  • PowerShell If -Not

PowerShell comparison operators  • PowerShell If -And  • PowerShell If -Or

Where Filter   • Loops  • Brackets  • -Match  • Switch  • Conditional Operators

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.

Download my ebook:Getting Started with PowerShell
Getting Started with PowerShell - only $9.25

You get 36 topics organized into these 3 sections:
   1) Getting Started
   2) Real-life tasks
   3) Examples of Syntax.

In addition to the ebook, you get a PDF version of this  Introduction to PowerShell ebook  It runs to 120 pages of A4.

 *


Custom Search

Guy Recommends: WMI Monitor and It's Free!Solarwinds WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft operating systems.

Fortunately, Solarwinds have created the Free WMI Monitor so that you can actually see and understand these gems of performance information.  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

 

Home Copyright © 1999-2012 Computer Performance LTD All rights reserved

Please report a broken link, or an error.