windowspowershellscriptingwindows-11windows-scripting

Can Windows 11 Sound and Display settings be scripted?


I'm helping a user with a personal Windows 11 computer which, in addition to the built-in monitor, is connected via HDMI to a shared/public television display.

When it comes time to stream a show or movie to the larger display, the following two operations must occur:

  1. Start > Settings > System > Sound > Output > Select correct device from dropdown list.
  2. Start > Settings > System > Display > Set display mode to "Extend" > Confirm changes.

Then, when steaming time is over, the Sound and Display settings must be set back to the more private configuration:

  1. Start > Settings > System > Sound > Output > Select built-in monitor from dropdown list.
  2. Start > Settings > System > Display > Set display mode to "Duplicate" > Confirm changes.

This "works" in that it produces the desired outcome, but is not "workable" for the non-technical user, given the number of touch points and values to remember. What would be great would be a pair of functionally equivalent scripts (one for each workflow) that I could link to as Desktop shortcuts.

Assuming full administrator access, is such a thing possible? Is there any API documentation I should be searching?


Solution

  • For display devices, Windows comes with a built-in command-line program displayswitch.exe (though do note that Windows 10 and Windows 11 have different, and incompatible, command-line options).

    For audio output, Windows doesn't seem to have a built-in command-line program nor PowerShell cmdlet, but NirSoft provides a reputable command-line program: nircmd setdefaultsounddevice.

    So combine the two:

    Start Streaming.cmd

    REM For Windows 11:
    REM DisplaySwitch.exe 1 = Internal
    REM DisplaySwitch.exe 2 = Clone
    REM DisplaySwitch.exe 3 = Extend
    REM DisplaySwitch.exe 4 = External
    
    DisplaySwitch.exe 3
    
    nircmd setdefaultsounddevice "Desired output device name" 1
    

    Stop Streaming.cmd

    DisplaySwitch.exe 1
    
    nircmd setdefaultsounddevice "Desired original output device name" 1