Switching Audio Input and Output With PowerShell
Thursday, December 11, 2008 – 10:09 PMSwitching between different audio input and output devices on Windows XP is pretty straightforward. You can do this using the Control Panel > Sounds, Speech, and Audio Devices > Sounds and Audio Devices tab (see right). It means opening this up and changing the appropriate dropdowns.
I wanted to implement be able to quickly switch between the onboard Realtek HD Audio sound and a Bluetooth handsfree audio device – in this case a Jawbone earpiece – on my Samsung NC10. There are a couple of tools that will switch sound devices. But it seemed like a good excuse to play around some more with PowerShell…
Switching devices in PowerShell
All the control panel dialog is doing under the hood is changing a few registry settings. What I wanted a script that would let me change from one profile to another.
$r = [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") $regRoot = "HKCU:\Software\Microsoft\"
$profiles = @{"Netbook" = @("Realtek HD Audio output", "Realtek HD Audio input"); "Bluetooth" = @("Bluetooth Hands-free Audio", "Bluetooth Hands-free Audio") } function Write-Message ( [string]$message ) { echo $message
# Uncomment this line to show dialog outputs from -set # $r = [System.Windows.Forms.MessageBox]::Show($message) } function Set-Mapping ( [string]$devOut, [string]$devIn ) { echo "Profile audio:`n in = $devIn`n out = $devOut" $regKey = $regRoot + "\Multimedia\Sound Mapper\" Set-ItemProperty $regKey -name Playback -value $devOut Set-ItemProperty $regKey -name Record -value $devIn } function List-Devices { $regKey = $regRoot + "\Windows\CurrentVersion\Applets\Volume Control\" echo "Sound devices:" ls $regKey | where { ! $_.Name.EndsWith("Options") } | Foreach-Object { echo (" " + $_.Name.Substring($_.Name.LastIndexOf("\")+1)) } } $cmd = $args[0] switch ($cmd) { "-profiles" { echo "Sound profiles:" echo $profiles } "-devices" { List-Devices } "-set" { $p = $args[1] if (!$profiles.ContainsKey($p)) { echo "No such profile: $p" echo $profiles exit } Set-Mapping $profiles.Get_Item($p)[0] $profiles.Get_Item($p)[1] Write-Message "Profile set to: $p" } default { Write-Message "No such option: $cmd" } }
As you can see it has three options:
- -profiles – List the available sound profiles.
- -devices – List the sound devices.
- -set [profile] – Set the audio profile.
You can add more profiles by updating the $profiles hash at the top of the script. You can setup one click switching by creating shortcuts on the desktop that run set commands:
%WINDIR%\system32\windowspowershell\v1.0\powershell.exe -command ".\SwitchAudio.ps1 -set Bluetooth"
I just have two shortcuts on my desktop to switch between the netbook speakers and bluetooth audio inputs and outputs. The only other thing to not is that many applications don’t detect that the default audio settings have changed after they start up so you may have to restart the application you’re using.
4 Responses to “Switching Audio Input and Output With PowerShell”
I can’t believe that I found your blog! I have an Acer AspireOne AOD250 and the sound works well. Portable speakers with a jack work. I just purchased a set of Insignia USB 2.0 portable speakers and I can not get the sound to come through the speakers. I can adjust the volume of the Acer with the buttons on the Insignia when it is all connected. I have done the usual; reinstalled the USB drivers, checked for Acer driver updates, called both support teams Acer and Insignia (they have no idea what’s wrong), have tried all three ports and tried powering the
Acer off and back on with and without the speakers plugged in. The speakers work when they are plugged into my emachine desktop USB port. Though I am somewhat computer literate, I know nothing of programming. This is my last ditch effort at trying to get these speakers to work. They are absolutely perfect for my job and I am hoping that you can walk me through the solution. Thank you for any ideas, suggestions or advice that you have for me.
By Christine on Jul 18, 2009
Genius! This is just what I was looking for. Thanks.
By Chris on Sep 17, 2009
I can\’t find howto do this in windows 7, and at that change from Speakers to Headphones.
Does anybody know howto do that (without using a external app.
By Simon on Sep 30, 2009
Not clear enough, not your fault but microsoft fault, i will keep on searching for a simpel freeware software program
By Mondriaan on Jan 31, 2010