Switching default audio device with a batch file

86,868

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.

Solution 4

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

Share:
86,868

Related videos on Youtube

zerothehero
Author by

zerothehero

Updated on September 18, 2022

Comments

  • zerothehero
    zerothehero almost 2 years

    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.

    • Admin
      Admin over 8 years
      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.]
  • LPChip
    LPChip over 8 years
    +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.
  • zerothehero
    zerothehero over 8 years
    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.
  • user2522503
    user2522503 about 5 years
    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/… )
  • user2522503
    user2522503 about 5 years
    +1. Tried the registry-approach, but it's not as easy as was suggested (at least with W10). Using NirCmd as well now :)
  • Pwnstar
    Pwnstar about 4 years
    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.
  • Albin
    Albin almost 4 years
    alternative post here (Win7)
  • Albin
    Albin almost 4 years
    You can copy and paste the device name by going to soundsettings -> device property and copying the device name from there (or/and renaming it). Or you can use this cmdlet to output the audiodevices (for the lazy copy&pasters): AudioDeviceCmdlets
  • Sam
    Sam over 3 years
    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.