Windows PowerShell BracketsIntroduction to Windows PowerShell - Type of BracketDid 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 Bracket1) 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 compulsory arguments. 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 {ScriptBlock}, which dictates the code to execute. Example 1: To demonstrate (parenthesis) and {braces} brackets# PowerShell cmdlet to demonstrate brackets Note 1: Observe how the parenthesis brackets enclose the compulsory statement ($objItem in $colItems), which starts the foreach loop. Note 2: Find the script block, enclosed by {braces}. This clause is important because it determines what to do inside each loop. Where the script block contains multiple lines, each half of the bracket has its own separate line, thus emphasising the action section of the script. 2) Braces Bracket { }A common place to pay attention to the braces style of PowerShell bracket is when you initiate a 'Where' clause. 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 [ ]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. Example 3a List all the processes beginning with the letter 's' get-Process [s]* Example 3b 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: Summary of PowerShell's BracketsIn 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 in looping constructions. Lastly, PowerShell's square [] brackets are used to control optional parameters. The most important lesson is that each type of bracket has a particular role; you must choose the correct bracket for the particular scripting task. See Also PowerShell Tutorials• PowerShell Home • If Statement • Conditional Operators • Switch • Loops • Brackets Please write in if you see errors of any kind. Please report any factual mistakes, grammatical errors or broken links, I will be happy to not only to correct the fault, but also to give you credit.
*
|
||||||