I simplified the source code to this, which when compiled works OK from a CMD line or from clicking on the .exe - but doesn't work from Task Scheduler.
Code:
; This is the "on" version
$windowName = "Sound"
$altPlusKey = "s"
$numDevices = 2
; Set device position to switch to 2 = Speaker
$ItemNumber = 2
; Run the sound control applet
Run("control mmsys.cpl")
; Wait a second for it to be active before sending keystrokes
Sleep (1000)
; Put the focus on the list
Send("{TAB}{TAB}{TAB}{TAB}")
; Tab down to the device position.
For $i = 1 to $ItemNumber Step 1
Send("{DOWN}")
Next
; Press shortkey to set the selected device as the default
Send("!"&$altPlusKey)
; Close the window.
WinClose($windowName)
I tried running that via a batch file - same result!