The PowerShell ISE sometimes behaves unpredictably after code changes are made

Viewed 780

I'm using the PowerShell ISE (PS version 5.0). If I run this code:

Write-Host "This"

It outputs:

This

If I modify the script like this:

Write-Host "That"

It outputs:

That

Great. As expected. Now, if I have this code:

$Form = New-Object System.Windows.Forms.Form
$Timer = New-Object System.Windows.Forms.Timer

$Timer.Add_Tick(
{
    &{
    Write-Output "Here"
    $Form.Close()} | Write-Host 
})

$Timer.Interval = 3000
$Timer.start()
$result = $Form.ShowDialog()

It outputs:

Here

If I change anything in the script, e.g. "Here" to "There" or $Timer.Interval = 3000 to $Timer.Interval = 4000 and run it, it does two unexpected things: 1.) instead of showing the form for the proper duration of time, it briefly flashes it on the screen, and 2.) it outputs the original Here instead of There. If I close the ISE and re-open it, the script runs as expected.

What is going on?

2 Answers
Related