Powershell: "Get-ChildItem | Format-Wide -AutoSize | Out-String" can't work on Powershell 6.0.1 & 6.0.2

Created on 23 Mar 2018  路  4Comments  路  Source: PowerShell/PowerShell

Steps to reproduce

Get-ChildItem | Format-Wide -AutoSize | Out-String

or

Get-ChildItem | Format-Wide -AutoSize | Out-String -Stream

Expected behavior



    Directory: C:\Users\andot\Documents\WindowsPowerShell\Modules\lolcat\1.0.1



.git                        LICENSE
lolcat.psd1                        lolcat.psm1
Out-Rainbow.psm1                   Out-StripAnsi.psm1
README.md                   screenshot.png

Actual behavior

out-lineoutput : Array dimensions exceeded supported range.
At line:1 char:1
+ Get-ChildItem | Format-Wide -AutoSize | Out-String -Stream
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [out-lineoutput], OutOfMemoryException
+ FullyQualifiedErrorId : System.OutOfMemoryException,Microsoft.PowerShell.Commands.OutLineOutputCommand

Environment data

> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      6.0.1
PSEdition                      Core
GitCommitId                    v6.0.1
OS                             Microsoft Windows 10.0.16299
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0
> $PSVersionTable                                                 
Name                           Value
----                           -----
PSVersion                      6.0.2
PSEdition                      Core
GitCommitId                    v6.0.2
OS                             Linux 4.4.0-43-Microsoft #1-Microsoft Wed Dec...
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

If no -AutoSize param, the result will be fine. And it works well in WindowsPowerShell 5.1 with or without -AutoSize param.

Issue-Bug Resolution-Fixed WG-Engine

Most helpful comment

Hi, I found an another workaround.

Workaround

Specify Out-String -Width parameter explicitly and set the appropriate value.

e.g.

Get-ChildItem | Format-Wide -AutoSize | Out-String -Width $Host.UI.RawUI.WindowSize.Width

Cause

When specify Format-Wide -AutoSize parameter, display items per row is decided from output width.
In this case, output width is depend on Out-String -Width value.

When -Width parameter is not specified, int.MaxValue is adopted as output width.
(This behavior is from PowerShell Core 6.0. #5193)

When initializing WideOutputContext, itemsPerRow value(=items per row) reaches int.MaxValue.
Therefore, creation of the _buffer buffer fails, resulting in an error.

https://github.com/PowerShell/PowerShell/blob/36b71ba39e36be3b86854b3551ef9f8e2a1de5cc/src/System.Management.Automation/FormatAndOutput/common/BaseOutputtingCommand.cs#L1135-L1170

All 4 comments

Hi, I found an another workaround.

Workaround

Specify Out-String -Width parameter explicitly and set the appropriate value.

e.g.

Get-ChildItem | Format-Wide -AutoSize | Out-String -Width $Host.UI.RawUI.WindowSize.Width

Cause

When specify Format-Wide -AutoSize parameter, display items per row is decided from output width.
In this case, output width is depend on Out-String -Width value.

When -Width parameter is not specified, int.MaxValue is adopted as output width.
(This behavior is from PowerShell Core 6.0. #5193)

When initializing WideOutputContext, itemsPerRow value(=items per row) reaches int.MaxValue.
Therefore, creation of the _buffer buffer fails, resulting in an error.

https://github.com/PowerShell/PowerShell/blob/36b71ba39e36be3b86854b3551ef9f8e2a1de5cc/src/System.Management.Automation/FormatAndOutput/common/BaseOutputtingCommand.cs#L1135-L1170

Thank you! Now on with Powershell 6.x, my lolcat Module can works well.

Could we use int.MaxValue-1 as init value?

Was this page helpful?
0 / 5 - 0 ratings