MCPMag.com

Sign up for our newsletter.

I agree to this site's Privacy Policy.

Prof. Powershell

PowerShell Syntax Elements Part 1: Parenthesis

Recently I received an e-mail from an IT Pro who had been going through my PowerShell training videos from Trainsignal (which is now part of Pluralsight). He was clearly just getting started with PowerShell, as I suspect many of you are as well. He was a little confused about PowerShell's use of syntax elements like (), {} and []. The question was when to use which. Using these elements is one of those PowerShell skills that you don't really think about but I is actually a very fair question. So let me devote a few lessons to taking the mystery out of these syntax elements. Today we'll look at parentheses.

The best analogy that comes to mind is a pair of hands. If you look at ( ) think of them as a pair of hands holding or cupping something. That is what they are doing in PowerShell. They are holding something, typically values or other objects. This "holding" can be further categorized. One such manner is grouping.

If you try an expression like this without parentheses you may not get the answer you expect.

PS S:\> 5+4/3*2+1
8.66666666666667
Instead, you probably meant something like this:
PS C:\> (((5+4)/3)*2)+1
7

PowerShell takes each thing you are holding in the parentheses and evaluates it from the inside out. If you were to trace it mentally it would look like this:

((9/3)*2)+1
(3*2)+1
6+1

Hopefully these are the same rules you learned in school. You might also use parentheses to group like this:

PS C:\> $var = "abcdef"
PS C:\> ($var -match "^a" -and $var.Length -eq 6) -or ($var -match "k$")
True

This is especially helpful with complex comparisons. Even if they aren't technically required, using them might make it easier to visually identify what the command is doing.

if ( ($var -match "^a") -or ($var -match "k$") ) {
"ok"
}

You will also see parentheses used with object methods. Again, they are holding something. In this situation, they are holding any parameter values for the specific method. Sometimes the method doesn't' need any parameters, but you still need to use ().

PS C:\> $n = "PowerShell Rules"
PS C:\> $n.ToUpper()
POWERSHELL RULES

Or if the method requires parameter values, they are held within the parentheses.

PS C:\> $n.Substring(5,5)
Shell

Figuring out what those values needs to be is a different matter. But here the values are passed to the Substring method and PowerShell displays the results.

I hope this little lesson helps. Next time we'll look at brackets. But in the meantime I think you'll find these elements easier to understand in context. If you learn that methods always use () you don't have to think about why. In any event, now you know.

About the Author

Jeffery Hicks is a Microsoft MVP in Windows PowerShell, Microsoft Certified Trainer and an IT veteran with over 20 years of experience, much of it spent as an IT consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He works today as an independent author, trainer and consultant. Jeff writes the popular Prof. PowerShell column for MPCMag.com and is a regular contributor to the Petri IT Knowledgebase and 4SysOps. If he isn't writing, then he's most likely recording training videos for companies like TrainSignal or hanging out in the forums at PowerShell.org. Jeff's latest books are Learn PowerShell 3 in a Month of Lunches, Learn PowerShell Toolmaking in a Month of Lunches and PowerShell in Depth: An Administrators Guide. You can keep up with Jeff at his blog http://jdhitsolutions.com/blog, on Twitter at twitter.com/jeffhicks and on Google Plus (http:/gplus.to/JeffHicks)

comments powered by Disqus

Reader Comments:

Add Your Comment Now:

Your Name:(optional)
Your Email:(optional)
Your Location:(optional)
Comment:
Please type the letters/numbers you see above