Setting playback device by executing a batch file / powershell script

powershell get default audio device
nircmd batch file
macro to change audio output
soundvolumeview
sound switcher
windows 10 select playback device shortcut
default devices
nircmd set volume

I've got my computer(Windows 7) hooked up to the TV, and i very often change output device for sound between Digital Audio (S/PDIF)(High definition audio device) and my headset (2- Corsair CA-HS1 USB Headset)

I wanna be able to execute a batch/script file who changes this for me so i don't have to "right click volume > playback devices > "Mark output device" and click "set default".

I know it's a luxury problem, but hey, maybe I can learn something from someone?

All help appreciated!

I had the exact same requirement as yourself, and AFTER stumbling across your posting I found the following:

https://web.archive.org/web/20131231034118/http://downloadsquad.switched.com/2010/06/16/windows-7-tip-how-to-change-the-default-audio-device-with-a-hot/

Unfortunately it's not a native Windows function; it requires the download of a small open-source scripting tool called AutoHotKey, but it works nicely and only requires a small amount of memory (1 ~ 2.5Mb)

The script provided in the original article doesn't work for me. It's searching for Enabled/Disabled devices and changing that value, as opposed to changing the default device. I've edited it to switch between 2 default devices now. It works by opening your Sound control panel (mmsys.cpl), then scrolling down the list of playback devices to the second item in the list (that's the {Down 2} part). This is because my Speakers are the second item in my list. It then checks to see if the device is default or not. If not, it sets it as the default and closes the window. If it's already the default, it scrolls down another 2 times and sets that as the default.

So, you'll need to ammend the {Down 2} lines to fit your own list of devices.

 #+a::
Run, mmsys.cpl
WinWait,Sound
ControlSend,SysListView321,{Down 2}
ControlGet, selectedDevice, List, Focused, SysListView321
Loop, Parse, selectedDevice, %A_Tab%
    if a_index <> 3
        continue
    else 
    {
        if A_LoopField <> Default Device
        {
            ControlClick,&Set Default
            ControlClick,OK
            WinWaitClose
            SoundPlay, *-1
            return
        }
        else
        {
            ControlSend,SysListView321,{Down 2}
            ControlClick,&Set Default
            ControlClick,OK
            WinWaitClose
            SoundPlay, *-1
            return
    }       
}

You need to make sure you use the exact name of your audio devices as listed under Playback Devices (right click the sound control The 1 at the end of the command signifies "Default Device". PC's audio configuration is stored in So powershell/bat script to toggle between 1 and 2 should do the trick. codereply.com has been informing visitors about topics such as Code for Java, Array and Source Code. Join thousands of satisfied visitors who discovered Java Code, Code and Code for Java.This domain may be for sale!

This is how I set 'Line 1' as the playback device:

start /min "" G:\......\nircmd.exe setdefaultsounddevice "Line 1"

NirCmd is a small command-line utility which you can download that allows you to do some useful tasks without displaying any user interface.

Open your Windows Playback devices (Right click on your speaker icon on You will need the exact same name for the NIRCMD command to work. but doesn't change the sound output until I click or run the bat file for a  Setting playback device by executing a batch file/powershell I wanna be able to execute a batch/script file who changes this for me so i don't have to "right click volume > playback devices > "Mark output device" and click "set default". I know it's

I had a HDMI device that keeps changing it's name, so none of the existing solutions worked for me.

I eventually ended up with this powershell and use of the NirCmd app.

#File: TV.ps1
$name = "SMART*"

# list active audio playback devices.   (Note for cature devices change Render to Capture)
$device = Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\MMDevices\Audio\Render\*\" | where {$_."DeviceState" -eq 1} | foreach-object -Process {(Get-ItemPropertyValue -Path ($_.PSPath + "\Properties\") -Name "{a45c254e-df1c-4efd-8020-67d146a850e0},2")} | Where-Object {$_ -like $name}

C:\bin\NIRCMDC setdefaultsounddevice $device 1
C:\bin\NIRCMDC setdefaultsounddevice $device 2

Tested by changing the default sound device on kiosk during an active session. Chrome out of fullscreen mode to mess with settings or run commands etc. It would be prefered to do this remotely via command line / PowerShell script or  If you want to run a script file with PowerShell, you have to change the execution policy on Windows 10. To change the execution policy to run PowerShell scripts, use these steps: Open

As far as I understand there is no way to do this programmatically. This is a deliberate design, since Microsoft does not want applications to override audio setting set by user.

You will find same answer here but if you solutions that manipulate windows you can have a look here.

Basic command-line audio device control from Powershell including Nuget For long-running scripts, it can be handy to get PowerShell to play a sound when Hi With a headset audio device I need to change settings like playback level or  Rather than hard-coding the entire path to the PowerShell script though, I recommend placing the batch file and PowerShell script file in the same directory, as my blog post describes. improve this answer. answered Nov 18 '13 at 22:23. 78 silver badges. 84 bronze badges. Invoke-WebRequest is working fine when I type the command line in a cmd

To follow up on Dale Newton's post, NirCmd is a great way to do this. On top of that if you pair it with AutoHotKey you can create an executable that will change your devices without opening pesky CMD windows every time you run it. For example, I have two sources that I switch between all the time, one is my headphones and they other is my monitor. For my monitor I created an ahk script that does this:

#NoEnv
SendMode Input
SetWorkingDir %A_ScriptDir% ;I have nircmd in the same folder as these scripts
Run, nircmd setdefaultsounddevice "Acer X34-8" 1
Run, nircmd setdefaultsounddevice "Acer X34-8" 2

And another for my headphones with the last two lines changed to:

Run, nircmd setdefaultsounddevice "Headset Earphone" 1
Run, nircmd setdefaultsounddevice "Headset Earphone" 2

Afterwards you can compile each ahk script into an exe and bind each exe to a keyboard macro so you can execute them with a couple key presses. Personally I am using a Corsair K95 so I use their software to bind these to my 'G' keys.

Also to note, if you are in your sound preferences you can rename any of the devices to avoid naming conflicts.

Summary: Microsoft Scripting Guy, Ed Wilson, talks about using The command and its associated output are shown here. Image of command output. Next, find the driver. Now that I know the name of the audio device, I can look for system Summary: Use Windows PowerShell to easily compare running  Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Learn more Setting playback device by executing a batch file / powershell script . Ask Question Asked 8 years, 2 months ago. Active 4 months

There is an alternate way to select the audio playback device for Under Settings -> System -> Sound, there's an option for “App volume and Create yourself a batch file so that you can run your PowerShell script with a  I am trying to run a .cmd file on a remote server with PowerShell. In my .ps1 script I have tried this: It results in this error: C:\MyDirectory\MyCommand.cmd is not recognized as the name of a cmdlet, function, script file, or operable program. Invoke-Command : Parameter set cannot be resolved using the specified named parameters.

"Windows 10 Setup Script" is a set of tweaks for OS fine-tuning and automating the I made a video of how to get it running: https://youtu.be/HgCOkMe6jBA Created a windows based system information tool in Go for Powershell and CMD​. On Windows 10, a batch file is a special kind of text file that typically has a .bat extension, which can include one or multiple commands that Command Prompt can understand and run in sequence to

Set volume and mute state of default audio device (playback/recording) It works with Win10, but sometime I run into some problems, but a quick restart of the application fixes it. Windows Package Manager Preview | Windows Command Line I recently had the pleasure of setting up PowerShell + VS Code on WSL2. Most of the time, you run Windows batch files using the Command Execution Method, which replicates running them in a command prompt window (cmd.exe). But, what if you need to perform additional processing within the same job and you are running that batch processing in PowerShell? You have several methods to launch Windows batch files from within PowerShell using these methods:

Comments
  • I'm fairly sure this can be done via WMI, so it should be trivial in PowerShell. I've only found Win32_SoundDevice so far, though, which doesn't really offer anything in that regard.
  • Yeah, that's about how far i came the last time i researched it too. I've been thinking that there must be some kind of software that can record commands beeing done when you for example click "all programs" in the start menu.. that would really be useful..
  • FYI I found that after installing AutoHotKey I started to get disconnected from some online games that use the PunkBuster anti-cheat system. I think PB was detecting the use of AutoHotKey and decided this was a vector for exploits. It might have been coincidence, or a false positive that has since been fixed, so mileage may vary.
  • This worked great for me, but the Set Default button in the Sound window will set your default sound device as well as your default communications device. If you want to leave your default communications device alone, replace ControlClick,&Set Default with Send {tab}{tab}{down}{down}{enter}. This will tab over to the Set Default button, open the dropdown menu, select Default Device, and press enter, leaving your communications device intact.
  • This is exactly what I was looking for! So easy. Thank you!
  • Perfect! NirCmd seems much more robust than UI automation with AutoHotKey. I had to rename one of my playback devices since both were just called "Speakers"
  • Works great for my Sony bluetooth headphones: nircmd.exe setdefaultsounddevice "Headphones"
  • It was included in my answer your codeplex project manipulate windows.
  • You can find the system device icons in System32\DDORes.dll: digitalcitizen.life/where-find-most-windows-10s-native-icons
  • If your audio outputs aren't configurable you'll need to change "{TAB 2}" to "{TAB 1}". If some of them are but others aren't, you'll need to add another command line parameter and mimic what's done for the "{DOWN "... section.
  • Save your script to a file that ends in .vbs, e.g. sound.vbs, put that in a directory, then create a Toolbar subdirectory where you put the shortcuts with the extra arguments (edit properties on the shortcut to set the parameters and change the icon), then right-click on the taskbar -> Toolbars -> New Toolbar, navigate to your new Toolbar subdirectory, that creates the new toolbar. Then right-click on it to not show the text or title, and voila!
  • Yeah I'm alreadu dooing so with Winamp and VLC, but I just as often use the TV and speaker system as my computer screens and headset.. :p