My goal is to monitor notepad process. If existing instance is closed then new one should start. I have following code (this is simplified snippet from my larger code base where I use WinForms):
function ShowRunningPids($startedProcesses)
{
    foreach ($proc in $startedProcesses){
        Write-Host ("`$proc.Id: {0}" -f $proc.Id)
    }
}
function CheckPids($procPid, $startedProcesses)
{
    Write-Host "Already started PIDS:"
    ShowRunningPids $startedProcesses
    $proc = Get-Process -Id $procPid -ErrorAction Ignore
    # PROBLEM: Not updating this PID means that after closing first instance of notepad the new instance is always spawned.
    # Because if statement is always checking against PID that was created as first.
    if (!$proc){
        Write-Host ("Process with PID: {0} not running - starting new process" -f $procPid)
        $processStatus = Start-Process -passthru -FilePath notepad
        Write-Host ("Process with PID: {0} started" -f $processStatus.Id)
        # PROBLEM: PID of closed notepad are not deleted from $startedProcesses
        [System.Collections.ArrayList]$startedProcesses = $startedProcesses | Where-Object { $_.Id -ne $procPid }
        $startedProcesses.add($processStatus)
        Write-Host ("Removed PID {0} and added PID {}" -f $procPid, $processStatus.Id)
    }
}
[System.Collections.ArrayList]$startedProcesses = @()
$processStatus = Start-Process -passthru -FilePath notepad
$startedProcesses.add($processStatus)
# start timer
$timer = New-Object System.Windows.Forms.Timer
    $timer.Interval = 1000
    # PROBLEM: How to update PID to instance of newly created notepad?
    $timer.add_tick({CheckPids $processStatus.id $startedProcesses})
    $timer.Start()
# Form
$objForm = New-Object System.Windows.Forms.Form 
    $objForm.Text = "Auto restart of process"
    $objForm.Size = New-Object System.Drawing.Size(330,380) 
    $objForm.StartPosition = "CenterScreen"
    $objForm.Add_Shown({$objForm.Activate()})
    $objForm.Add_Closing({$timer.Stop(); })
    [void] $objForm.ShowDialog()
After I close existing instance of notepad the new one is started every second until I stop script. I can see several problems here which are described in comments in above code. Also I get following error message on console:
Cannot convert the "System.Diagnostics.Process (notepad)" value of type "System.Diagnostics.Process" to type "System.Collections.ArrayList".
At C:\Users\wakatana\PS\ps.ps1:19 char:9
+         [System.Collections.ArrayList]$script:startedProcesses = $scr ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : MetadataError: (:) [], ArgumentTransformationMetadataException
    + FullyQualifiedErrorId : RuntimeException
Error formatting a string: Input string was not in a correct format..
At C:\Users\wakatana\ps.ps1:21 char:9
+         Write-Host ("Removed PID {0} and added PID {}" -f $procPid, $ ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (Removed PID {0} and added PID {}:String) [], RuntimeException
    + FullyQualifiedErrorId : FormatError
How to fix this code?