1

I have this script:

Write-Host " start `n"

Get-LocalUser | Where-Object {$_.Enabled} | Write-Output

Write-Host "end `n"

I want the output to look like this:

start

Name    Enabled Description
----    ------- -----------
user    True    account info
student True    account info

end

But for some reason, when I run the script, I get this:

start


end

Name    Enabled Description
----    ------- -----------
user    True    account info
student True    account info

Why is it waiting until after the script to display the info, and not doing it where I called the command (i.e. in between "start" and "end")?

I have tried assigning it to a variable and using write-host to print the variable... that did not work.

I also tried using write-host at the beginning instead of piping to write-output... that did not work either.

New to PowerShell scripting, what did I do wrong?

  • 1
    The lack of synchronization between pipeline output and to-host output (as well as other output streams) is limited to PS v5+ and a very specific - albeit still common - scenario: implicitly _table_-formatted output for types that do _not_ have formatting data defined for them. The - suboptimal - workaround is to force the pipeline output synchronously to the host (display) with `Out-Host` - see [this answer](https://stackoverflow.com/a/43691123/45375). – mklement0 Oct 16 '21 at 02:35
  • 1
    That worked, thank you for explaining and providing a solution. – Kennycampbell1 Oct 16 '21 at 02:41
  • 1
    Your use of `Write-Output`, though not the issue, is redundant here. The success stream (which `Write-Output` writes to) is what gets written to by default. That's not to say it's *always* redundant or unnecessary, but your use of it here is. Read [this answer of mine](https://stackoverflow.com/a/58615047/584676) to learn more about the different output streams, what they mean, and how to use them effectively. – codewario Oct 16 '21 at 02:43
  • Glad to hear it,@Kennycampbell1; my pleasure. – mklement0 Oct 16 '21 at 03:14

0 Answers0