C# events never handled in PowerShell 5
Write-Output
does not display object on console, it send object to the next command in pipeline. If you want immediately display some text on console, then you have to use Write-Host
or Out-Host
cmdlet.
"Never use Write-Host because evil!"
I would say it this way: "Never use Write-Host on intermediate data because evil!". It is OK to display some progress notification with Write-Host
(although you can consider of using Write-Progress
instead) or use it to display final result, but Write-Host
send nothing to next command in pipeline, so you can not process data any further.
Why do all the other Write-Output commands write to the console?
When some object reach ultimate end of pipeline, PowerShell have to do something about it. By default it display it on host (console in this case), although you can override this:
New-Alias Out-Default Set-Clipboard # to copy anything into clipboardNew-Alias Out-Default Out-GridView # to use grid view by default
PowerShell does not support of script block invocation in some arbitrary thread, thus for asynchronous events you have to use Register-ObjectEvent
, so PowerShell can handle them.