question

herngyih avatar image
0 Votes"
herngyih asked ·

Powershell custom objects

Hi Folks
I was wondering why a PS custom object will not be displayed according to sequence? It will be displayed last no matter what.

$table= @() $table += New-Object psobject -Property @{SID ="ola";MG="$MG"} $table write-host "lol"

Output:
Notice that the string "lol" displayed first regardeless

lol
SID MG


ola 2d

windows-server-powershell
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered ·

It has nothing to do with PSCustomObject. The statement "$table" is sent to stream 1 (the "Success" stream). Write-Host is sending the output directly to the host. If you want them written in the order you expect, use Write-Output instead of Write-Host.

understanding-streams-redirection-and-write-host-in-powershell


· 1 · Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you. will definately give a read

0 Votes 0 ·
BillStewart avatar image
0 Votes"
BillStewart answered ·

This is likely an implementation detail (there's a difference between standard output and host-only output).

· Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

js2010 avatar image
0 Votes"
js2010 answered ·

It's hard to understand your code. But usually objects are implicitly sent through format-table, which has a slight delay so it can see how to align columns.

· Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered ·

Hi,

The object $table is sent to the pipeline that the script is runing and will be piped to Out-Default by default
https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/out-default?view=powershell-7.1

· Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.