In most C-like languages I am deliberate about placing my braces where I think they make the code easiest to read.
PowerShell's parser recognizes when a statement clearly isn't complete, and looks to the next line. For example, imagine a cmdlet that takes an optional script block parameter:
Get-Foo { ............ }
if the script block is very long, you might want to write:
Get-Foo
{
...............
...............
...............
}
But this won't work: the parser will see two statements. The first is Get-Foo and the second is a script block. Instead, I write:
Get-Foo {
...............
...............
...............
}
I could use the line-continuation character (`) but that makes for hard-to-read code, and invites bugs.
Because this case requires the open brace to be on the previous line, I follow that pattern everywhere:
if (condition) {
.....
}
Note that
ifstatements require a script block in the language grammar, so the parser will look on the next line for the script block, but for consistency, I keep the open brace on the same line.
Simlarly, in the case of long pipelines, I break after the pipe character (|):
$project.Items |
? { $_.Key -eq "ProjectFile" } |
% { $_.Value } |
% { $_.EvaluatedInclude } |
% {
.........
}