34

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.

1
  • 1
    If you give up on not wanting too use 3rd party, I've been using audioswit.ch/er [yes that's the real URL] for about 5 years. Hot key switchable too. [no affiliation.]
    – Tetsujin
    Mar 19 '16 at 16:50
43

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

6
  • +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
    Mar 18 '16 at 18:50
  • 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. Mar 18 '16 at 22:02
  • +1. Tried the registry-approach, but it's not as easy as was suggested (at least with W10). Using NirCmd as well now :)
    – MBaas
    Jun 5 '19 at 9:40
  • This does not work for Windows 10: Added setdefaultsounddevice command (for Windows 7/Vista/2008 only), which allows you to set the default sound device. May 5 '20 at 15:24
  • alternative post here (Win7)
    – Albin
    Sep 13 '20 at 10:40
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

1
  • 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 groovypost.com/howto/… )
    – MBaas
    Jun 5 '19 at 8:37
3

Powershell solution to Toggle between two specific audio devices

This requires that you download and install the AudioDevice commandlets from here: https://github.com/frgnca/AudioDeviceCmdlets

Follow their installation instructions and check that the commandlets are working in your Powershell.

Then you can use this script:

<#
Use 
   Get-AudioDevice -List
to learn about available devices on your system.
Then set the two variables below with the start of their names.
#>
$device1 = "PHILIPS"
$device2 = "Speakers"

$Audio = Get-AudioDevice -playback
Write-Output "Audio device was " $Audio.Name
Write-Output "Audio device now set to " 

if ($Audio.Name.StartsWith($device1)) {
   (Get-AudioDevice -list | Where-Object Name -like ("$device2*") | Set-AudioDevice).Name
}  Else {
   (Get-AudioDevice -list | Where-Object Name -like ("$device1*") | Set-AudioDevice).Name
}

Then you can set that script as a shortcut in your Taskbar or wherever. Here is an example shortcut I use:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -windowstyle hidden C:\Tools\ToggleSoundDevice.ps1

You can also find many solutions to attach a shortcut key to this.

1
  • 2
    This is insane how you posted this answer rigth in the day I started to google this problem! Best solution so far, works like a charm.
    – Sam
    Feb 11 at 17:58
1

Here is a nice working AHK script using Nircmd that will do the job just perfectly with a small notification in the bottom right corner.

Thanks to @Jonno

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.