PowerShell script to toggle between two Windows audio output devices from the comand line
Install AudioDeviceCmdlets
Module
-
Open PowerShell as Administrator:
- Press
Win + X
and selectWindows PowerShell (Admin)
or search for PowerShell in the Start menu and right-click to run it as administrator.
- Press
-
Install the Module:
Install-Module -Name AudioDeviceCmdlets -Repository PSGallery -Force
- List Audio Devices:
Import-Module AudioDeviceCmdlets Get-AudioDevice -List
This command will list all audio devices with their IDs and names. Take note of the Index
values as you will need them later.
Next, download the ToggleAudioDevice.ps1
file from GitHub, preferably into your $PATH
.
Now, open the ToggleAudioDevice.ps1
file and define the speaker indices found in the list above.
# Define the indices of your devices
$device1Index = 3 # Headphones
$device2Index = 4 # Speakers
Now you can run the .\ToggleAudioDevice.ps1
script from PowerShell, and even bind it as a macro to your keyboard!