Switching default audio device with a batch file

Solution 1:

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

Solution 2:

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

Solution 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.