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
Instead, you probably meant something like this:
PS C:\> (((5+4)/3)*2)+1

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:


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$")

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$") ) {

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()

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

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

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 an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at, and a frequent speaker at technology conferences and user groups.

comments powered by Disqus

SharePoint Watch

Sign up for our newsletter.

I agree to this site's Privacy Policy.