> What would be the right thing to use to write to the Windows equivalent of stdout?
**In *effect*, but very *unfortunately***, both **Windows PowerShell and PowerShell Core as of v7.2, send _all_ of their 6(!) [output streams](
[To see links please register here]
) to _stdout_** when **called _from the outside_, via PowerShell's [CLI](
[To see links please register here]
)**.
* See [GitHub issue #7989](
[To see links please register here]
) for a discussion of this problematic behavior, which likely won't get fixed, so as to preserve backward compatibility.
* **In practice, this means that _whatever PowerShell stream you send output to_ will be seen as _stdout_ output by an external caller:**
* E.g., if you run the following from `cmd.exe`, you'll see _no_ output, because the stdout redirection to `NUL` applies equally to all PowerShell streams:
* `C:\>powershell -noprofile -command "Write-Error error!" >NUL`
* However - curiously - **if you _redirect stderr_, PowerShell _does_ send its error stream to stderr**, so that with `2>` you _can_ capture the error-stream output selectively; the following outputs just `'hi'` - the success-stream output - while capturing the error-stream output in file `err.txt`:
* `C:\>powershell -noprofile -command "'hi'; Write-Error error!" 2>err.txt`
The **_desirable_ behavior** is:
* Send PowerShell's **_success output stream_ (number `1`) to _stdout_**.
* Send output from **_all other streams_ to _stderr_**, which is the only option, given that _between_ processes only _2_ output streams exist - stdout (standard output) for _data_, and stderr (standard error) for error messages _and_ all other types of messages - such as status information - that aren't _data_.
* **It's advisable to make this distinction in your code, even though it currently isn't being respected**.
---
**_Inside_ PowerShell**:
* **`Write-Host`** is **for _display_ output**, and **_bypasses_ the success output stream** - as such, its output can neither be (directly) captured in a variable nor suppressed nor redirected.
* Its original intent was simply to create user feedback and create simple, console-based user interfaces (colored output).
* Due to the prior inability to be captured or redirected, PowerShell version 5 made `Write-Host` write to the newly introduced _information stream_ (number `6`), so since then it _is_ possible to capture and redirect `Write-Host` output.
* **`Write-Error`** is meant for **writing _non-terminating_ errors to the _error stream_** (number `2`); conceptually, the error stream is the equivalent of stderr.
* **`Write-Output`** writes to the **_success [output] stream_** (number `1`), which is conceptually equivalent to stdout; it is **the stream to write _data_ (results) to.**
* However, **explicit use of `Write-Output` is rarely needed** due to PowerShell's **_implicit output_ feature**:
* **Output from any command or expression that isn't explicitly captured, suppressed or redirected is _automatically_ sent to the success stream**; e.g., `Write-Output "Honey, I'm $HOME"` and `"Honey, I'm $HOME"` are equivalent, with the latter not only being more concise, but also faster.
* See [this answer](
[To see links please register here]
) for more information.