Why doesn't my monitor swapping script work?

Hi, I wrote a couple of scripts to switch screen and speakers from my monitor/Bluetooth speaker to a TV/speakers in an adjoining room. Here are the scripts.

The first one redirects the visual and audio signal to the TV

Displayswitch.exe /external

$device1 = “Sony TV” $device2 = “Speakers”

$Audio = Get-AudioDevice -playback Write-Output "Audio device was " $Audio.Name Write-Output "Audio device now set to "

if ($Audio.Name.StartsWith($device1)) { (Get-AudioDevice -list | Where-Object Name -like ("$device2*") | Set-AudioDevice).Name } Else { (Get-AudioDevice -list | Where-Object Name -like ("$device1*") | Set-AudioDevice).Name }

The second one loads Emulationstation, waits until it closes then switches the output back to the computer monitor.

Start-Process “path to” Emulationstation.exe

Wait-Process -Name Emulationstation -ErrorAction SilentlyContinue

Displayswitch.exe /Internal

All good so far. Click the icon (monitors swap) click the second icon and play. The script resets everything when I exit Emulationstation.

So what’s the problem you ask? Well when I combine the scripts into a single script the sound does not transfer to the TV. Can anyone give me any tips as to why?

I made some additions to the second script.

Start-Process “path to” JoytoKey.exe

Write-Output “JoytoKey loaded…”

Start-Process “path to” Emulationstation.exe -wait

Write-Output “Emulationstation closed…”

Taskkill /F /IM JoytoKey.exe

Write-Output “JoytoKey closed…”

Displayswitch.exe /Internal

Write-Output “Monitor set to internal…”

I new get a slightly different problem, the sound transfers ok but Emulationstation opens a 4k window (My primary monitor is 4k and my TV is 1080p) so I can only see part of the screen.

I’m totally confused as to why. It continues to work fine as separate scripts.