Switching default audio device with a batch file

22

10

I'm trying to write a batch file on Windows 10 that allows me to switch between my headset and my speakers as default audio device when I run it. I don't want to use any third-party software.

I tried searching around but i only found old scripts that don't seem to work and also refer to a HKCU:\Software\Microsoft\Windows\CurrentVersion\Applets\Volume Control\ path that doesn't seem to exist anymore. I cant't find any information on the Windows 10 register about this, also I'm not comfortable with fiddling too much with the register if I'm not sure of what i'm doing.

zerothehero

Posted 2016-03-18T17:44:50.687

Reputation: 221

1

If you give up on not wanting too use 3rd party, I've been using http://audioswit.ch/er [yes that's the real URL] for about 5 years. Hot key switchable too. [no affiliation.]

– Tetsujin – 2016-03-19T16:50:54.993

Answers

33

I appreciate you don't wish to use any third party software, but as an option for if you don't mind using a ~100kb exe, you can use Nircmd with the commands:

nircmd setdefaultsounddevice "Speakers" 1

or

nircmd setdefaultsounddevice "Headphones" 1

You need to make sure you use the exact name of your audio devices as listed under Playback Devices (right click the sound control in the system tray). It may be easier to rename them under Properties to simpler names, especially if the names clash in any way.

The 1 at the end of the command signifies "Default Device". Using 2 signifies "Default Communications Device".


If you really don't want to use a third party tool, here's a diff of a registry key that seems to change when I change my default sound device, it may be of some use to you, but I don't know exactly what it's doing. It's not as simple as a 1 or a 0 to indicate if it's a default, it would seem.

enter image description here

Jonno

Posted 2016-03-18T17:44:50.687

Reputation: 18 756

+1 from me. This is the best way to go. If you really don't want to have nircmd placed somewhere on your harddrive (believe me you want it, its great) then the alternative would be to switch it twice and make a reg export of the key mentioned here and execute the right .reg file. Not sure if this will work though, but that would be my alternative approach. – LPChip – 2016-03-18T18:50:43.697

Thanks for the answer, I actually stumbled upon nircmd myself, but was really curious as to finding a way to do that without any third party software. If no other method comes out, I'll consider using it. – zerothehero – 2016-03-18T22:02:55.207

+1. Tried the registry-approach, but it's not as easy as was suggested (at least with W10). Using NirCmd as well now :) – MBaas – 2019-06-05T09:40:18.107

6

Since Windows 8 (or maybe earlier?) PC's audio configuration is stored in HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\MMDevices\Audio\Render. Under Render there are GUID-named keys, each containing DeviceState dword value at root. But how is it coded?

See DEVICE_STATE_XXX Constants at MSDN:

  • 1 Active
  • 2 Disabled
  • 4 Not present
  • 8 Unplugged

So powershell/bat script to toggle between 1 and 2 should do the trick.

EDIT: To get human-readable device name, read {b3f8fa53-0004-438e-9003-51a46e139bfc},6 under Properties subkey

Aziz Kabyshev

Posted 2016-03-18T17:44:50.687

Reputation: 169

Hi Aziz, I tried this under W10, but it only worked once - and when I tried to switch to previous device via batch, it suddenly disappeared from the list that you get when clicking the tray-icon for audio. Also I found that I needed to get permissions for that registry-key first (see https://www.groovypost.com/howto/take-full-permissions-control-edit-protected-registry-keys/ )

– MBaas – 2019-06-05T08:37:50.030