haakonlu haakonlu - 3 months ago 17
PowerShell Question

Setting playback device by executing a batch file / powershell script

I've got my computer(Windows 7) hooked up to the TV, and i very often change output device for sound between Digital Audio (S/PDIF)(High definition audio device) and my headset (2- Corsair CA-HS1 USB Headset)

I wanna be able to execute a batch/script file who changes this for me so i don't have to "right click volume > playback devices > "Mark output device" and click "set default".

I know it's a luxury problem, but hey, maybe I can learn something from someone?

All help appreciated!

Answer

I had the exact same requirement as yourself, and AFTER stumbling across your posting I found the following:

http://downloadsquad.switched.com/2010/06/16/windows-7-tip-how-to-change-the-default-audio-device-with-a-hot/

Unfortunately it's not a native Windows function; it requires the download of a small open-source scripting tool called AutoHotKey, but it works nicely and only requires a small amount of memory (1 ~ 2.5Mb)

The script provided in the original article doesn't work for me. It's searching for Enabled/Disabled devices and changing that value, as opposed to changing the default device. I've edited it to switch between 2 default devices now. It works by opening your Sound control panel (mmsys.cpl), then scrolling down the list of playback devices to the second item in the list (that's the {Down 2} part). This is because my Speakers are the second item in my list. It then checks to see if the device is default or not. If not, it sets it as the default and closes the window. If it's already the default, it scrolls down another 2 times and sets that as the default.

So, you'll need to ammend the {Down 2} lines to fit your own list of devices.

 #+a::
Run, mmsys.cpl
WinWait,Sound
ControlSend,SysListView321,{Down 2}
ControlGet, selectedDevice, List, Focused, SysListView321
Loop, Parse, selectedDevice, %A_Tab%
    if a_index <> 3
        continue
    else 
    {
        if A_LoopField <> Default Device
        {
            ControlClick,&Set Default
            ControlClick,OK
            WinWaitClose
            SoundPlay, *-1
            return
        }
        else
        {
            ControlSend,SysListView321,{Down 2}
            ControlClick,&Set Default
            ControlClick,OK
            WinWaitClose
            SoundPlay, *-1
            return
    }       
}
Comments