DEV Community

CodeWithCaen
CodeWithCaen

Posted on • Originally published at tips.desilva.se

4

Toggle Windows audio output devices from the command line

Toggle between Windows audio output devices from the command line!

Here's a simple PowerShell script to toggle between two Windows audio output devices using the AudioDeviceCmdlets module. This script can be run from the command line or even bound as a macro to your keyboard for quick switching.

The full GitHub project is at https://github.com/caendesilva/WindowsToggleAudioDevice

Installation

Install AudioDeviceCmdlets Module

  1. Open PowerShell as Administrator:

    • Press Win + X and select Windows PowerShell (Admin) or search for PowerShell in the Start menu and right-click to run it as administrator.
  2. Install the Module:

   Install-Module -Name AudioDeviceCmdlets -Repository PSGallery -Force
Enter fullscreen mode Exit fullscreen mode

List All Audio Devices

  1. List Audio Devices:
   Import-Module AudioDeviceCmdlets
   Get-AudioDevice -List
Enter fullscreen mode Exit fullscreen mode

This command will list all audio devices with their IDs and names. Take note of the Index values as you will need them later.

Download ToggleAudioDevice.ps1

Next, download the ToggleAudioDevice.ps1 file from GitHub, preferably into your $PATH.

You can also just copy the following file contents, just note that it may not be up to date!

# https://github.com/caendesilva/WindowsToggleAudioDevice/blob/master/ToggleAudioDevice.ps1

Import-Module AudioDeviceCmdlets

# Define the indices of your devices
$device1Index = 3
$device2Index = 4

# Get the current default audio playback device index
$currentDeviceIndex = (Get-AudioDevice -Playback | Where-Object { $_.Default -eq "True" }).Index

Write-Host "Current device index: $currentDeviceIndex"

# Function to set the default audio playback device by index
function Set-DefaultAudioDeviceByIndex {
    param (
        [int]$deviceIndex
    )

    Set-AudioDevice -Index $deviceIndex
    Write-Host "Switched to device index $deviceIndex."
}

# Toggle the default audio playback device
if ($currentDeviceIndex -eq $device1Index) {
    Set-DefaultAudioDeviceByIndex -deviceIndex $device2Index
} else {
    Set-DefaultAudioDeviceByIndex -deviceIndex $device1Index
}
Enter fullscreen mode Exit fullscreen mode

Configure ToggleAudioDevice.ps1

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
Enter fullscreen mode Exit fullscreen mode

Usage

Now you can run the .\ToggleAudioDevice.ps1 script from PowerShell, and even bind it as a macro to your keyboard!


This PowerShell script provides a quick and easy way to toggle between your preferred audio devices. Make sure to adjust the device indices in the script to match your specific setup, and you'll be switching audio outputs in no time!

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay