You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Asynchronous behavior of implicit Format-Table use can have surprising side effects - console output may appear out of order #4594
In short, he states that, starting in PSv5, an implicitly applied Format-Table call now waits 300 ms. before producing output in order to collect a reasonable number of objects to output so as to better determine suitable column widths.
That means that subsequent commands get to execute before the Format-Table output kicks in, which results in surprising behavior if these commands produce non-pipeline display output (e.g., with Write-Host) or prompt for user input, for instance.
Update: A related problem (irrespective of whether the asynchronous output is a problem) is that the implicit output formatting applied to the first command in a script locks in the implicit formatting for all remaining commands, which, if Format-Table is locked in, can lead to objects output by subsequent commands "disappearing", as originally detailed in #4552 (e.g., [pscustomobject] @{ one = 1; two = 2}; [pscustomobject] @{ three = 3} makes the 2nd custom object "disappear", because it doesn't share any properties with the 1st one) - see also: #12825
Steps to reproduce
[pscustomobject] @{ one=1 } # implicit Format-Table call - in PSv5+, waits 300 ms. before outputting'hi'|Out-Host# explicit, synchronous, non-pipeline outputRead-Host-Prompt 'Press any key'
Expected behavior
PSv4- produces (note how the implicit Format-Table output comes after the prompt):
one
---
1
hi
Press any key:
Actual behavior
PSv5+ produces:
hi
Press any key:
one
---
1
Environment data
PowerShell Core v6.0.0-beta.5 on macOS 10.12.6
PowerShell Core v6.0.0-beta.5 on Ubuntu 16.04.3 LTS
PowerShell Core v6.0.0-beta.5 on Microsoft Windows 10 Pro (64-bit; v10.0.15063)
Windows PowerShell v5.1.15063.483 on Microsoft Windows 10 Pro (64-bit; v10.0.15063)
Reacted by irvnriir, zett42, Axel D., Santiago Squarzon and iRon7
Re as-designed: I assumed as much, but it's another gotcha that calls for Documentation-Needed.
Re -AutoSize: Note that the problem only occurs with implicit use of Format-Table. If you use ... | Format-Table explicitly - whether with or without -AutoSize - there is no asynchronous behavior.
Re -AutoSize: Note that the problem only occurs with implicit use of Format-Table. If you use ... | Format-Table explicitly - whether with or without -AutoSize - there is no asynchronous behavior.
& {
[pscustomobject] @{ one=1 } # implicit Format-Table call - in PSv5+, waits 300 ms. before outputting'hi'|Out-Host# explicit, synchronous, non-pipeline outputRead-Host-Prompt 'Press any key'
} |Format-Table
Good point, but what I meant was applying Format-Table directly to the command that would otherwise trigger the asynchronous behavior (under the assumption that that command does not have embedded pipeline-bypassing host operations):
Now that I think about it, even that use of Format-Table to force synchronous output can be problematic, because - while fine for display - it replaces the original objects with formatting objects in the pipeline.
Indeed it is, thanks - due to its generic title, I didn't discover it.
Given that this issue describes the problem more specifically and includes a command to reproduce it, can I suggest we instead close #1747 as a duplicate?
Isn't that the expected result of the delay in Format-Table to get widths for auto-size?
Do people want to give up that feature to "fix" this oddness in output?
This is the side effect of the change we made. Lee Holmes (@LeeHolmes) made the initial change so that we can compute more usable column widths based on objects returned within the first [N units of time].
changed the title [-]Asynchronous behavior of implicit Format-Table use can have surprising side effects[/-][+]Asynchronous behavior of implicit Format-Table use can have surprising side effects - console output may appear out of order[/+]on Aug 18, 2017
Since this behavior happens only with [pscustomobject] do you suggest using [psobject] as an alternative since it doesn't have this problem?
I know psobject is somewhat slower and works slightly differently (ex. doesn't retain the order of members) but I it's still a good workaround, since there is no need to use hackery and should be easy to update existing code that makes use of pscustomobject.
metablaster, the problem isn't specific to [pscustomobject] (which is technically the same as [psobject] - System.Management.Automation.PSObject), it affects all (non-primitive) .NET types that (a) do not have predefined formatting data associated with them and (b) trigger implicit Format-Table formatting due to having 4 or fewer properties; e.g.:
PS> [regex] ''; write-host hi # !! 'hi' prints first
hi
Options RightToLeft MatchTimeout
------------------------------
None False -00:00:00.0010000
As an aside: Use of [psobject] in PowerShell code is best avoided; see this comment for background information on the unfortunate conflation of [psobject] and [pscustomobject].
I took a look at your link where you said Use [System.Collections.Generic.List[object]], for my specific example in previous duplicate issue #14464 I tried with Dictionary<TKey,TValue> which unfortunately results in same unexpected behavior.
However your mention of "format data" solved all of my problems and I'm still sticking with PSCustomObject
I defined Format.ps1xml file with custom type and now all problems are gone 😄
Thanks so much!
foreach ($iin1..3)
{
Write-Information-MessageData "Performing operation $i"-INFA "Continue"
[PSCustomObject]@{
TemplateKey1="A$i"TemplateKey2="B$i"TemplateKey3="C$i"# Cutom type name for Format.ps1xmlPSTypeName="TestType"
}
}
Glad to hear it. Yes, associating formatting data with any type avoids the problem.
Re:
you said Use [System.Collections.Generic.List[object]]
I should have made it clearer that this was an entirely incidental aspect of why I linked to that comment - I linked to it solely for the background information on [psobject] in the second section.
A more insidious side effect of the asynchronous behavior is that CLI calls from outside PowerShell can result in data loss due to missing output - see this Stack Overflow question and
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commented on Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commented on Nov 23, 2023
Activity
SteveL-MSFT commentedon Aug 16, 2017
The current behavior is
by design. If this is a functional issue, I suppose we could have-AutoSize:$falsenot have the timermklement0 commentedon Aug 16, 2017
Steve Lee (@SteveL-MSFT):
Re as-designed: I assumed as much, but it's another gotcha that calls for
Documentation-Needed.Re
-AutoSize: Note that the problem only occurs with implicit use ofFormat-Table. If you use... | Format-Tableexplicitly - whether with or without-AutoSize- there is no asynchronous behavior.Or are we talking about different things?
PetSerAl commentedon Aug 16, 2017
iSazonov commentedon Aug 16, 2017
It seems internally we call Format-Default in the case.
mklement0 commentedon Aug 16, 2017
PetSerAl:
Good point, but what I meant was applying
Format-Tabledirectly to the command that would otherwise trigger the asynchronous behavior (under the assumption that that command does not have embedded pipeline-bypassing host operations):Now that I think about it, even that use of
Format-Tableto force synchronous output can be problematic, because - while fine for display - it replaces the original objects with formatting objects in the pipeline.iSazonov commentedon Aug 17, 2017
It is dup #1747
mklement0 commentedon Aug 18, 2017
Ilya (@iSazonov):
Indeed it is, thanks - due to its generic title, I didn't discover it.
Given that this issue describes the problem more specifically and includes a command to reproduce it, can I suggest we instead close #1747 as a duplicate?
Here's its summary:
Joel Bennett (@Jaykul) states:
Steve Lee (@SteveL-MSFT) states:
Jason Shirk (@lzybkr) states:
Joey Aiello (@joeyaiello) states:
[-]Asynchronous behavior of implicit Format-Table use can have surprising side effects[/-][+]Asynchronous behavior of implicit Format-Table use can have surprising side effects - console output may appear out of order[/+]iSazonov commentedon Aug 20, 2017
Currently I don't know what way is better.
(maint) Change winrm specs to write to stdout
(maint) Change winrm specs to write to stdout
(maint) Change winrm specs to write to stdout
(maint) Change winrm specs to write to stdout
18 remaining items
metablaster commentedon Dec 19, 2020
Since this behavior happens only with
[pscustomobject]do you suggest using[psobject]as an alternative since it doesn't have this problem?I know
psobjectis somewhat slower and works slightly differently (ex. doesn't retain the order of members) but I it's still a good workaround, since there is no need to use hackery and should be easy to update existing code that makes use ofpscustomobject.mklement0 commentedon Dec 19, 2020
metablaster, the problem isn't specific to
[pscustomobject](which is technically the same as[psobject]-System.Management.Automation.PSObject), it affects all (non-primitive) .NET types that (a) do not have predefined formatting data associated with them and (b) trigger implicitFormat-Tableformatting due to having 4 or fewer properties; e.g.:As an aside: Use of
[psobject]in PowerShell code is best avoided; see this comment for background information on the unfortunate conflation of[psobject]and[pscustomobject].metablaster commentedon Dec 19, 2020
Michael Klement (@mklement0)
I took a look at your link where you said
Use [System.Collections.Generic.List[object]], for my specific example in previous duplicate issue #14464 I tried withDictionary<TKey,TValue>which unfortunately results in same unexpected behavior.However your mention of "format data" solved all of my problems and I'm still sticking with
PSCustomObjectI defined Format.ps1xml file with custom type and now all problems are gone 😄
Thanks so much!
Actual AND expected behavior
mklement0 commentedon Dec 19, 2020
metablaster:
Glad to hear it. Yes, associating formatting data with any type avoids the problem.
Re:
I should have made it clearer that this was an entirely incidental aspect of why I linked to that comment - I linked to it solely for the background information on
[psobject]in the second section.mklement0 commentedon Oct 3, 2021
A more insidious side effect of the asynchronous behavior is that CLI calls from outside PowerShell can result in data loss due to missing output - see this Stack Overflow question and
microsoft-github-policy-service commentedon Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 23, 2023
This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.
jszabo98 commentedon Nov 24, 2023
Glad to see all these bugs have been resolved.
metablaster commentedon Nov 24, 2023
lol, reminds me of one reason for not resolving a bug from GNU mailing list labeled as "Nobody cares"