#28013·PowerShell

Running a script using `pwsh -File` splits multi-line output strings

Author: Wonderer0Created Sep 14, 2026Updated Sep 16, 2026
LabelsResolution-Answered

Prerequisites

Steps to reproduce

If you have a simple Powershell script, e.g. Test-Splits-Output.ps1, containing just:

Write-Output "Multi`nLine`nMessage"

and run it in a Powershell console:

.\Test-Splits-Output.ps1 *>&1 | ForEach-Object { "[$($_.GetType().Name)]" ; "<<<$_>>>" }

the output is a single string as expected:

[String]
<<<Multi
Line
Message>>>

But if you run it as a file:

pwsh -f .\Test-Splits-Output.ps1 *>&1 | ForEach-Object { "[$($_.GetType().Name)]" ; "<<<$_>>>" }

the output is split into multiple one line strings

[String]
<<<Multi>>>
[String]
<<<Line>>>
[String]
<<<Message>>>

The effect is similar for the error stream.

Write-Error "Multi`nLine`nMessage"

when run as a file:

pwsh -f .\Test-Splits-Error.ps1 *>&1 | ForEach-Object { "[$($_.GetType().Name)]" ; "<<<$($_.Exception.Message)>>>" }

is arguably worse in that it outputs multiple [ErrorRecord] objects for a single error:

[ErrorRecord]
<<<Test-Splits-Error.ps1: Multi>>>
[ErrorRecord]
<<<Line>>>
[ErrorRecord]
<<<Message>>>

Running it using pwsh -c '.\Test-Splits-Error.ps1' also has the same effect.

Why is this happening? Is it because pwsh is a native command so its output is being treated as binary data which is then converted back into objects when it is piped into the ForEach-Object?

Expected behavior

That `pwsh -f myScript.ps1` and `pwsh -c myScript.ps1` should give the same output as running `myScript.ps1` directly, apart from the issues documented in about_Pwsh.

Actual behavior

They split each single output object into multiple ones.

Error details

Environment data

Name                           Value
----                           -----
PSVersion                      7.6.6
PSEdition                      Core
GitCommitId                    7.6.6
OS                             Microsoft Windows 10.0.19045
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.4
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visuals

No response