r/PowerShell • u/Nanis23 • Jul 13 '26
Question The worst thing in Powershell existence is ForEach-Object syntax
The way I format my code is like this: if there is only one line inside the curly braces, I put the opening curly brace on the same line.
If I have multiple lines, I put the opening curly brace on the next line.
For example:
if (a=a) {
Command-Command}if (a=a)
{
Command-Command
Command-Command
}
I have been doing this for years, and it works every time, except when I use "ForEach-Object".
Then, for some reason, the opening curly brace must be on the same line.
Why is it like this??
0
Upvotes
16
u/surfingoldelephant Jul 13 '26 edited Oct 10 '26
No, it works every time you use language keywords like
ifandforeach. These get parsed in expression mode, which is more flexible with white space.ForEach-Objectis a command. Commands get parsed in argument mode, which requires parameters/arguments start on the same line. Otherwise, the parser has no way of knowing if the argument is just another statement unrelated to the command.The error you get from this:
Or the mandatory parameter prompt from this:
...comes from parameter binding, which is a runtime concept that happens long after parsing. Both examples are syntactically valid, hence the need for the parameter and/or argument to start on the same line so the parser knows it's a command invocation with a parameter/argument.
Also, there's nothing special about
ForEach-Object. It's the exact same for any command with any parameter/argument. You can't do this for the exact same reason:Or this...
Whereas keywords get parsed in expression mode, which knows that an opening
{must come next for it to be syntactically valid, so white space limitations are relaxed.PowerShell style guide recommends One True Brace Style (OTBS) mainly because of the white space limitations in argument mode.
2
u/rickAUS Jul 13 '26 edited Jul 13 '26
Don't use Where-Object or -ScriptBlock in Invoke-Command? They have the same constraint because they're expecting the open brace to be on the same line to be considered part of the command. I don't really know why since other aspects of PowerShell don't have this limitation but it is what it is I guess.
And for what it's worth, the accepted community standard is brace on the same line as the keyword, not on a new line. But people will still do either since it largely supports both except under 3 situations.
2
u/purplemonkeymad Jul 13 '26
Because Foreach-Object is a command not a part of syntax. The curly brace is literally part of a parameter for that command. (Do you know there is a -begin and -end parameter as well, and that you can add as many script blocks as you want?)
You could alternatively create an advanced function for your pipeline instead, then you don't even need the code at that point and have it elsewhere.
2
u/PinchesTheCrab Jul 13 '26
If we were on the same team we'd be playing tug of war on commits, lol. That formatting would drive me up the wall and my auto-formatting settings in my IDE would pull the brace back up onto the 'correct' line over and over.
Like other people said, the script block is a parameter of a cmdlet named foreach-object. You couldn't do something like:
It's the same with foreach-object. If that's jarring, I think the path of least resistance is to follow suit with your other syntax. In my personal, anecdotal experience, you're an outlier if you do this:
It's not wrong, but I think you've encountered one of its limitations, and if a junior team member had to maintain what you've written while you were sick, you can see how they might get confused by what read like (but isn't) a syntax shift.
2
u/BlackV Jul 13 '26 edited Jul 13 '26
Personally you are being inconsistent in your formatting
You could format your code all with a hanging indent style
Or
Or
Or
that was jsut an example, it's better to pick a style and keep it standard, rather than switching between them all the time
for example set vsc code or use a specific style when formatting (allerman, otb, etc)
5
u/joeykins82 Jul 13 '26
Because
ForEach-Objectis meant as a quick'n'dirty option when you're doing something simple with stuff in the pipeline. If you're ever doing something more complex than "just do this 1 low-complexity thing for every pipeline object" you should construct a properForEach ($objThing in $arrThings) { ... }block.