0

I'm writing Powershell (v5.1) trying to ouptut a PSCustomObject, and then a few lines later output a hashtable. However it appears that outputting the PSCustomObject is preventing the Hashtable from outputting properly. Why does this happen?

This is a minimal example:

$foobar = [PSCustomObject]@{foo = 'bar'}
Write-Output $foobar # Comment out this line and it outputs wizwam

$wizwam = @{'wiz' = 'wam' }
Write-Output $wizwam

This is the output:

foo
---
bar

But I would have expected this output:

foo
---
bar

Name                           Value
----                           -----
wiz                            wam

Interestingly, if I comment out the second line (Write-Output $foobar), then it does output WizWam properly:

Name                           Value
----                           -----
wiz                            wam

Also, if I swap the order of foobar and wizwam:

$wizwam = @{'wiz' = 'wam' }
Write-Output $wizwam

$foobar = [PSCustomObject]@{foo = 'bar'}
Write-Output $foobar

Then it outputs both wizwam and foobar as expected:

Name                           Value
----                           -----
wiz                            wam

foo : bar

What is going on?

2
  • 2
    If it's exclusive for display output, you will need to add a pipe to Out-Host or Out-Default to both calls of Write-Output Commented Mar 29, 2022 at 1:11
  • 2
    Probably has to do how PowerShell parses the script, so piping to Out-Default should ensure it get's output. Commented Mar 29, 2022 at 1:13

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.