Why "Write-Host" works differently from PowerShell function and timer handler?

Viewed 75

I have 1809 Windows 10 box with PowerShell Core 6.1.1 Given following code sample:

function Test() {
  Write-Host "Test"
}
function Invoke-Test() {
  $timer = New-Object System.Timers.Timer
  $timer.AutoReset = $false
  $timer.Interval = 1
  Register-ObjectEvent -InputObject $timer -EventName Elapsed -Action {
    Test
  }
  $timer.Enabled = $true
}

If I invoke "Test" function, I get "Test" output as expected:

enter image description here

But if I schedule invocation with a timer, command prompt is completely messed up:

enter image description here

I vaguely understand that it's something related to internal "readline" and console mechanic, but is it any way to produce newline output followed by a command prompt from a timer/handle in powershell?

1 Answers
Related