Categories
AI Answers Archive Desktop Home Automation Notes

Changing Screens in Windows via script

TL;DR: .bat scripts below to set video (via native DisplaySwitch.exe) and sound output (via nircmd). To avoid black PC monitor on startup, switch back before shutdown.


It has been a long time bugging me. I use my PC to drive a 34 inch Asus monitor, and sometimes, I use it to drive my TV. However, switching between the two is cumbersome. Sometimes, default audio is not switched and I have to do it manually. And on my TV, sitting on the couch, I have neither keyboard nor mouse. So what I do is, I use Unified Remote on my smartphone. There it’s quite easy to change display, and you have a good mouse, by using your smartphone as a touchpad.

Today I thought, i write two scripts, that make it even easier, and maybe, i can run one or the other after start, so that the correct output is set on startup (because when you forget it on TV, nothing is visible, once you start your PC, when you sit on your monitor).

Anyhow, the scripts require some 32bit/64bit windows quirks, and thanks to Google Gemini Pro 3.1, here they are.

Note: you need the nircmd tool: https://www.nirsoft.net/utils/nircmd.html

Note 2: If you’re using Unified Remote, activate the “Start Application” remote, and put the scripts in this folder:

  • cmd+r: shell:startup — then two folders up in “Start Menu”
  • This will be a folder like this: C:\Users\[youruser]\AppData\Roaming\Microsoft\Windows\Start Menu

Note 3: If you use your TV, and then shutdown, be aware, that you have still selected TV, and the next day you will stare in a black screen. To avoid that, Gemini’s idea, which I think makes sense, is to switch back to PC, and THEN shutdown.

tv.bat

@echo off
echo Switching video output to Second Screen Only...

:: Use Sysnative to bypass Unified Remote's 32-bit redirection
if exist "%windir%\Sysnative\DisplaySwitch.exe" (
    "%windir%\Sysnative\DisplaySwitch.exe" /external
) else (
    "%windir%\System32\DisplaySwitch.exe" /external
)

:: Wait 3 seconds for the TV to wake up and register its audio channel
timeout /t 3 /nobreak >nul

echo Switching audio output to Samsung TV...
:: Using absolute path for NirCmd to prevent PATH errors
C:\apps\nircmd.exe setdefaultsounddevice "SAMSUNG"

echo Done!

pc.bat

@echo off
echo Switching video output to PC Screen Only...

:: Use Sysnative to bypass Unified Remote's 32-bit redirection
if exist "%windir%\Sysnative\DisplaySwitch.exe" (
    "%windir%\Sysnative\DisplaySwitch.exe" /internal
) else (
    "%windir%\System32\DisplaySwitch.exe" /internal
)

:: Wait 3 seconds for the monitor to register its audio channel
timeout /t 3 /nobreak >nul

echo Switching audio output to Asus Monitor...
:: Using absolute path for NirCmd to prevent PATH errors
C:\apps\nircmd.exe setdefaultsounddevice "Asus"

echo Done!

shutdown-switchback.bat

@echo off
echo Switching video output to PC Screen Only...

:: Use Sysnative to bypass Unified Remote's 32-bit redirection
if exist "%windir%\Sysnative\DisplaySwitch.exe" (
    "%windir%\Sysnative\DisplaySwitch.exe" /internal
) else (
    "%windir%\System32\DisplaySwitch.exe" /internal
)

:: Wait 3 seconds for the monitor to register its audio channel
timeout /t 3 /nobreak >nul

echo Switching audio output to Asus Monitor...
:: Using absolute path for NirCmd to prevent PATH errors
C:\apps\nircmd.exe setdefaultsounddevice "Asus"

:: Wait 2 seconds to ensure the display switch registers
timeout /t 2 /nobreak >nul

echo Shutting down the PC...
:: /s = shutdown, /t 0 = wait 0 seconds
shutdown /s /t 0

Leave a Reply

Your email address will not be published. Required fields are marked *