Start only once

A Friend of mine had the problem that in his Simulator a couple of programs needed to be started – but only once.
And in case some of his mates started some of them multiple times, everything got screwed up.

So i offered him to write a little startup script in Powershell which first checks, whether the program was already running before it just starts it.

Here it is:

<# 
    You can add more programms to start into the programs list, but each will be checked, whether it was already started, so each program in this list will be started only once.
    If you want to figure out, with which name you need to add your program, just execute `get-process > processes.txt` within powershell and check the names within the processes.txt you created.
#>

$programs = @(

    # opens the notepad editor. If you want to load a file, add the ArgumentList containing the Path to the file to open.
    [pscustomobject]@{
        Name = "ImmersiveDisplayPRO";
        FilePath = "C:\Program Files (x86)\ImmersiveDisplayPRO\ImmersiveDisplayPRO.exe";
        WorkingDirectory = "C:\Program Files (x86)\ImmersiveDisplayPRO";
    }

    [pscustomobject]@{
        Name = "FlightSimulator";
        FilePath = "H:\WindowsApps\Microsoft.FlightSimulator_1.18.14.0_x64__8wekyb3d8bbwe\FlightSimulator.exe";
        WorkingDirectory = "H:\WindowsApps\Microsoft.FlightSimulator_1.18.14.0_x64__8wekyb3d8bbwe\";
    }
    
    [PSCustomObject]@{
        Name = "FSUIPC7";
        FilePath = "H:\FSUIPC7\FSUIPC7.exe"; 
        WorkingDirectory = "H:\FSUIPC7\";
    }
    
    [PSCustomObject]@{
        Name= "LINDA";
        FilePath = "H:\FSUIPC7\LINDA.exe"; 
        WorkingDirectory = "H:\FSUIPC7\";
    }

    [PSCustomObject]@{
        Name = "Navigraph Charts";
        FilePath = "C:\Users\Joe\AppData\Local\Programs\Navigraph Charts\Navigraph Charts.exe";
        WorkingDirectory = "C:\Users\Joe\AppData\Local\Programs\Navigraph Charts";
    }
    [PSCustomObject]@{
        Name = "TrackIR5";
        FilePath = "C:\Program Files (x86)\NaturalPoint\TrackIR5\TrackIR5.exe";
        WorkingDirectory = "C:\Program Files (x86)\NaturalPoint\TrackIR5\";
    }
)

# Do not modify here:
$programs | ForEach-Object {
    $prog = $_.FilePath
    $progName = $_.Name
    $Running = get-process -ErrorAction SilentlyContinue | Where-Object { $_.Name -eq $progName }
    if (!$Running) { 
        write-host "Starting: " $prog
        Start-Process $prog -WorkingDirectory ($_.WorkingDirectory ?? "")
    } else {
        write-host "  ignoring: " $prog " as it seems to run"
    }
}

#Close the window after everything ran through
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown');
stop-process -Id $PID