Your first example works, because you tell PowerShell that the statement is continued in the next line (by escaping the linebreak).
The other two examples fail, because the expression on the first line is a complete expression in and by itself, whereas the expression on the second line is not. PowerShell first evaluates ($_.Responding -ne $true)
, then tries to evaluate -or ($_.ProcessName -like 'W*')
, which is an invalid statement and thus fails.
To avoid this you need to make it clear to the interpreter that the statement on the first line is continued in the next line. That can be done either by escaping the linebreak like you do in your first example:
Get-Process | Where-Object {
($_.Responding -ne $true) `
-or ($_.ProcessName -like 'W*')
}
or by constructing your statement in a way that allows the interpreter to detect that there is more to come, e.g. by putting the operator at the end of the first line instead of at the beginning of the second line:
Get-Process | Where-Object {
($_.Responding -ne $true) -or
($_.ProcessName -like 'W*')
}
The curly braces can't be used to detect the end of the statement, because it's valid to put multiple statements in a Where-Object
scriptblock.