Powershell: Pipe Generates Error From Multiple Script Outputs

Created on 20 Jun 2019  路  3Comments  路  Source: PowerShell/PowerShell

Steps to reproduce

1 | Out-File Script.ps1
( ./Script.ps1; ./Script.ps1 ) | % { $_ } # Generates error
# PS> $( ./Script.ps1; ./Script.ps1 ) | % { $_ } # Works (hack)

Expected behavior

1
1

Actual behavior

At line:1 char:15
+ ( ./Script.ps1; ./Script.ps1 ) | % { $_ }
+               ~
Missing closing ')' in expression.
At line:1 char:30
+ ( ./Script.ps1; ./Script.ps1 ) | % { $_ }
+                              ~
Unexpected token ')' in expression or statement.
At line:1 char:32
+ ( ./Script.ps1; ./Script.ps1 ) | % { $_ }
+                                ~
An empty pipe element is not allowed.
+ CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingEndParenthesisInExpression

Environment data

Name                           Value
----                           -----
PSVersion                      6.2.1
PSEdition                      Core
GitCommitId                    6.2.1
OS                             Microsoft Windows 10.0.17763
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0鈥
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0
Issue-Question Resolution-Answered

All 3 comments

The pipe isn't the problem here.

( ./Script.ps1; ./Script.ps1 )

This isn't valid syntax. Explicit linebreaks (semicolons) are not permitted inside parentheses _unless_ those parentheses form a subexpression $( ... ) or an array subexpression @( ... )

This would work in a scriptblock as well, but you'd need to invoke the scriptblock with &:

& { ./script.ps1; ./script.ps1 } | % { $_ }

@vexx32 Thank you for the response.

I would have expected that I can use semi-colons within brackets since I can use pipes within them. For example, ( ./Script.ps1 | ./Script.ps1 ).

Understandable... Not sure what the rationale is there to be honest. Perhaps @bpayette could speak to that. :smile:

Was this page helpful?
0 / 5 - 0 ratings