-
Notifications
You must be signed in to change notification settings - Fork 7.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Why do headers only appear once? #2228
Comments
Hopefully I can get some clarification on what the issue is here. The 'name' and 'value' header are printed only once because it is printing a table of your hashtable. |
The hashtable if printed three (3) times. Only the first has headers. |
I see this behavior in Powershell 5.1 (Windows 10) as well. |
Short answer: that's just how PowerShell formatting system works. Let's take a look at this code $h = @{1 = 'a'; 2 = 'b'}
$h
Write-Host '======='
$h
Write-Host '======='
$h There are two separate routes in this code that both are ending up on the screen:
Note that these two methods are deeply different in Powershell. PS C:\src\powershell> $a = ./headers.ps1
=======
=======
PS C:\src\powershell> $a
Name Value
---- -----
2 b
1 a
2 b
1 a
2 b
1 a As you can see, you can manipulate So we can take
Now, we need to talk about PowerShell formatting system. Every object type has formatting rules. They are applied in two cases:
There are two most used formatting groups:
It's important to point out, that although every object has default formatting, it's not locked just to this formatting. We are one step away from finishing the explanation. The last step is "why there is only one header for table formatting"?
@Liturgist does it answer your question? |
@vors - It sounds like if the next object to be formatted is the same type as the previous object, then the formatter does not emit any headers. Is that right? If so, then the formatters do not actually go into what many people think of as the pipeline; stdout. There are many Write-* cmdlets. Would the following items seem to be similar for someone coming from a traditional C/*NIX environment? Are there other streams you would add here? How many default streams does PowerShell have?
If I were to want to have headers written each time the formatter is invoked, what kind of things might I do? I tried changing Write-Host to Write-Output, but have the same result. Having a [string] in the pipeline did not reset the formatter to produce headers. Does Write-Output go through the formatter?
Just found what appears to be a great article about PowerShell and streams. https://blogs.technet.microsoft.com/heyscriptingguy/2014/03/30/understanding-streams-redirection-and-write-host-in-powershell/ |
I cannot come up with a good use-case, but if you really want to have headers, you can do
Formatter emits only one header for table, regardless of the types of objects.
It may seems that information is lost in this example (the There are 6 streams, i.e.
1 - stdout I could mess up 3, 4, 5 maybe they have a different order. :) The one that you named |
From https://connect.microsoft.com/PowerShell/feedback/details/297055/capture-warning-verbose-debug-and-host-output-via-alternate-streams Is this the way it is, or the way Keith Hill is suggesting it should be?
|
The streams are as follows
Which I took from the about_redirection help file - The Connect Item was from 2007 which would be in the v1 - v2 time line and v2 had a number of functional updates over v1 |
Is this behavior documented in help system? |
@thezim yes https://technet.microsoft.com/en-us/library/hh847746.aspx @Liturgist it almost works as described in the Connect
In general, streams concepts are not that central in PowerShell compare to the object pipeline. Particularly, all streams higher than 2 are one or another form of information logging. The stream number really represents severity (less is more important). I'd like to quote Jeffrey one more time
|
@vors I was referring to the formatter behavior. Sorry for not being clear. |
You can use
|
@lzybkr - I would not really want to have a header and header separator for each and every line. |
You wouldn't get headers for every line - you'll just get headers as though no more objects are being written to the pipe. This way, the next object won't try and apply the same formatting, the formatter will choose the default formatting for this new object. In the following example, in 113, notice how the
|
The original question appears now well-answered, and I do not see anyone waiting for a follow-up, so I'm closing the issue. Thanks for participating @Liturgist! |
This tripped me up when I wanted to do polling: while ($true) {
Clear-Host
&$Command
Start-Sleep 1
} The first time the headers are rendered, the second time and following they disappear. This works around it: while ($true) {
Clear-Host
&$Command | Format-Table
Start-Sleep 1
} I believe |
@felixfbecker please open that as a separate issue |
Why does the header line ("Name" and "Value") only appear the first time?
The text was updated successfully, but these errors were encountered: