2

I'm writing a PowerShell script to check some properties of a server. When I execute the following cmdlets in order, the output is not displayed in the order I expect.

Write-Host "=== Operating System ==="
Get-WmiObject -computername $ServerName -class Win32_OperatingSystem | select Caption | ft -HideTableHeaders -AutoSize

Write-Host "=== Pagefile ==="
Get-WmiObject -ComputerName $ServerName -Class Win32_PageFileSetting

Write-Host "=== Locale ==="
Get-WmiObject -ComputerName $ServerName -class Win32_OperatingSystem | select Locale | ft -HideTableHeaders -AutoSize | Write-Output

Output:

=== Operating System ===

Microsoft Windows Server 2012 Standard

=== Pagefile ===

=== Locale ===

MaximumSize Name Caption
----------- ---- -------
4000 C:\pagefile.sys C:\ 'pagefile.sys'

0813

How does it come that the pagefile output is not displayed under its title? It seems to me that Get-WmiObject is running asynchronously and immediately writes the next title (Locale).

How can I prevent this? It works when using Write-Output, but then I cannot use colored text.

1
  • Try piping the second Get-WmiObject call to |ft explicitly like you do with the others - also, don't pipe formatted output to write-output May 21, 2015 at 16:55

1 Answer 1

1

Some of your text on-screen comes from objects going through the PowerShell pipeline, and some comes from Write-Host which prints to the screen immediately without touching the pipeline.

Gathering WMI information about the pagefile takes time and outputs to the pipeline, but the script has already moved on to printing the Locale message before that pipeline has finished.

This doesn't happen for (Operating System), because that pipeline finishes with ft -AutoSize (Format-Table), which pauses the script and waits for everything in the pipeline so it has as much information as possible when deciding how to size the columns. Once ft prints to the screen, then the script moves to the PageFile message.

You should use Write-Output instead to send the text through the pipeline, this way it will integrate well with other PowerShell features like redirection, collecting command output in files and variables.

Jeffrey Snover (PowerShell lead architect) has written about why Write-Host is not recommended on his blog here: http://www.jsnover.com/blog/2013/12/07/write-host-considered-harmful/

Here's a link describing the behaviour of Format-Table:

Optimizing Column Width

Because the pipeline processes results in real time, PowerShell cannot know how wide of a space the column elements will occupy. As a result, it will tend to be generous in sizing columns. If you specify the -AutoSize parameter, Format-Table will collect all results first before setting the maximum width for all elements. You can optimize output, but the results will no longer be output in real time:

Master-PowerShell | With Dr. Tobias Weltner

4
  • Format-Table indeed did the trick. Is there a way to use colors with Write-Output as well?
    – EsTeGe
    May 22, 2015 at 6:48
  • @EsTeGe Not really. That's like asking if you can use colours in a text text file or store coloured text in a variable. If you only want to show text on screen then Write-Host is the tool for the job - but if you want status messages from a script as it progresses, Write-Output is a better habit to get into, then the script output can be logged to a file to see what happened or stored in a variable and checked, and so on. You could use Write-Warning and Write-Error which show up in different colours - but they are separate channels intended for logging warnings and errors. May 23, 2015 at 2:46
  • Sure, I understand that analogy, but maybe there could be a way that piping coloured text to a file would simply ignore the colour, but still show the colours on the screen. Think of it like printing a coloured text in black & white on paper. Anyway, I'm using Write-Output now.
    – EsTeGe
    May 23, 2015 at 11:53
  • @EsTeGe What colour is $x="hello" in memory? You could include colour information inside the text - write HTML <p style='color:red'>hello</p> and view in a web browser, for example. Or RichText and view in WordPad. Or ANSII colour codes and view in a third party Linux style terminal. The text is still colourless though; it always will be, and if you output it to a text file the display markup metadata won't be ignored because it's part of the text. May 23, 2015 at 18:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .