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.
Get-WmiObject
call to|ft
explicitly like you do with the others - also, don't pipe formatted output to write-output