I cannot achieve to split an "if" condition over multiple lines in PowerShell WITH comments, see example:
If ( # Only do that when...
$foo # foo
-and $bar # AND bar
)
{
Write-Host foobar
}
This generates the following error:
Missing closing ')' after expression in 'if' statement.
Adding the `
character does not work:
If ( ` # Only do that when...
$foo ` # foo
-and $bar ` # AND bar
)
{
Write-Host foobar
}
I get a:
Unexpected token '` # foo' in expression or statement.
The only way I've found is to remove the comments:
If ( `
$foo `
-and $bar `
)
{
Write-Host foobar
}
But I am sure PowerShell offers a way to do what others scripting languages can: I just seem cannot find it...
Long if statements may be split onto several lines when the character/line limit would be exceeded. The conditions have to be positioned onto the following line, and indented 4 characters. The logical operators ( && , || , etc.)
The recommended style for multiline if statements in Python is to use parentheses to break up the if statement. The PEP8 style guide recommends the use of parentheses over backslashes and putting line breaks after the boolean and and or operators.
You can have a string split across multiple lines by enclosing it in triple quotes. Alternatively, brackets can also be used to spread a string into different lines. Moreover, backslash works as a line continuation character in Python. You can use it to join text on separate lines and create a multiline string.
The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. Long lines can be broken over multiple lines by wrapping expressions in parentheses. These should be used in preference to using a backslash for line continuation.
PowerShell automatically wraps lines when it recognizes an incomplete statement. For comparison operations this is the case if for instance you write a line with a dangling operator:
if ( # Only do that when...
$foo -and # foo AND
$bar # bar
)
Otherwise PowerShell will parse the two lines as two different statements (because the first line is a valid expression by itself) and fail on the second one because it's invalid. Thus you need to escape the linebreak.
However, just putting an escape character somewhere in the line won't work, because that will escape the next character and leave the linebreak untouched.
$foo ` # foo
Putting it at the end of a line with a (line) comment also won't work, because the comment takes precedence, turning the escape character into a literal character.
$foo # foo`
If you want to escape the linebreaks you need to either move the comment elsewhere:
if (
# Only do that when foo AND bar
$foo `
-and $bar
)
or use block comments as @Chard suggested:
if ( # Only do that when...
$foo <# foo #> `
-and $bar <# AND bar #>
)
But frankly, my recommendation is to move the operator to the end of the previous line and avoid all the hassle of escaping linebreaks.
You can use block comments <# Your Comment #>
to do this.
If ( <# Only do that when... #> `
$foo <# foo #> `
-and $bar <# AND bar #>
)
{
Write-Host foobar
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With