Solving Problems with Parenthesis

by Aug 29, 2011

You use parenthesis in PowerShell to control an execution order. However, some language keywords are not legal inside parenthesis, like try and catch. See what happens with these two lines:

try { 1 } catch { 2 }
(try { 1 } catch { 2 })

The second line will fail with an exception. To work around this, you can use $() instead:

$(try { 1 } catch { 2 })

With $(), you can group commands without the language restrictions that () imposes.

Twitter This Tip!
ReTweet this Tip!