41

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
  • 2
    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
    Commented Mar 19, 2016 at 16:50

8 Answers 8

50

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

9
  • +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
    Commented Mar 18, 2016 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. Commented Mar 18, 2016 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
    Commented Jun 5, 2019 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.
    – Pwnstar
    Commented May 5, 2020 at 15:24
  • alternative post here (Win7)
    – Albin
    Commented Sep 13, 2020 at 10:40
14

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.

2
  • 3
    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
    Commented Feb 11, 2021 at 17:58
  • 2
    Thanks. Installed via Install-Module -Name AudioDeviceCmdlets as Admin (!) PS, And triggered via AHK Script RunWait % "PowerShell -ExecutionPolicy Bypass -File .\switch-devices.ps1",, Hide Commented Feb 25 at 18:08
7

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

2
  • 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
    Commented Jun 5, 2019 at 8:37
  • I get access denied if I try to change something there even by using an admin command line.... Commented Aug 25, 2023 at 23:49
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

1

More reliable solution for AudioDevice commandlets. https://github.com/frgnca/AudioDeviceCmdlets

Thanks to @pgr for original answer.

Using ID instead names is more reliable with language localisations.

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

if ($Audio.Index -EQ 1) {
   (Get-AudioDevice -list | Where-Object Index -EQ 2 | Set-AudioDevice).Name
}  Else {
   (Get-AudioDevice -list | Where-Object Index -EQ 1 | Set-AudioDevice).Name
}
1
0

Download nircmd from the internet, then:

:: Set Sound Devices
NIRCMD setdefaultsounddevice "VoiceMeeter Input" 1^
    & NIRCMD setdefaultsounddevice "VoiceMeeter Input" 2^
    & NIRCMD setdefaultsounddevice "ANovaMic" 1^
    & NIRCMD setdefaultsounddevice "ANovaMic" 2

I use these tiny scripts from my stream deck, not only for streaming but also to switch among devices any time.

0

You can do it with SoundVolumeView from NirSoft.

  1. Sort by Name, then Device Name

  2. Select Speakers Device Render

  3. Right Click - Create Desktop Shortcut

  4. Set Default Device - All

  5. Open Shortcut Properties & Copy Target

  6. "C:\tools\NirLauncher\NirSoft\soundvolumeview.exe" /SetDefault "Realtek(R) Audio\Device\Speakers\Render" all

Just run the shortcut, or paste the shortcut target in batch file.

0

Here is a more reliable way using SoundVolumeView.exe or SoundVolumeView's command line version svcl.exe

Just setting the device by name is not safe because some devices have same name and in that case NirCmd will use the first one it finds in the devices list.

The best way that I discovered after playing around the SoundVolumeView.exe is to use /SetDefault option.

Here is the full command

svcl.exe /SetDefault "Apple Audio Device\Device\Speakers\Render" all

Here the "Apple Audio Device\Device\Speakers\Render" is my device Command-Line Friendly ID and you can get it from the list by listing all the devices using svcl.exe /scomma or you can use the SouldVolumeView.exe GUI and use the Command-Line Friendly ID.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .