Skip to content

Instantly share code, notes, and snippets.

@pchinjr
Created June 6, 2024 19:01
Show Gist options
  • Save pchinjr/c9f366bc94d50058492936f1277d56b0 to your computer and use it in GitHub Desktop.
Save pchinjr/c9f366bc94d50058492936f1277d56b0 to your computer and use it in GitHub Desktop.
This script toggles the default audio output device between headphones and speakers on a Windows system.
# SwitchAudio.ps1
# This script toggles the default audio output device between headphones and speakers on a Windows system.
# Documentation:
# This script uses the AudioDeviceCmdlets PowerShell module to manage audio devices.
# It identifies the current default audio device and switches to the other specified device.
# Dependencies: AudioDeviceCmdlets module
# Step-by-step guide to set up and use this script:
# 1. Install AudioDeviceCmdlets Module
# Open PowerShell as an administrator and run the following command to install the AudioDeviceCmdlets module:
# Install-Module -Name AudioDeviceCmdlets -Scope CurrentUser -Force
# 2. Install BurntToast Module
# Run the following command to install the BurntToast module:
# Install-Module -Name BurntToast -Scope CurrentUser -Force
# 3. Discover Audio Devices
# Run the following command to list all audio devices and find the exact names of your headphone and speaker outputs:
# Get-AudioDevice -List
# 4. Update Device Names
# Replace "Realtek HD Audio 2nd output" and "Realtek High Definition Audio" with the names of your actual devices.
# Import necessary modules
Import-Module -Name AudioDeviceCmdlets
Import-Module -Name BurntToast
# Define the names of your audio devices
$headphones = "Realtek HD Audio 2nd output (Realtek(R) Audio)" # Replace with your headphone device name
$speakers = "LG Ultra HD (NVIDIA High Definition Audio)" # Replace with your speaker device name
# Function to set the default audio device
function Set-DefaultAudioDevice {
param (
[string]$deviceName
)
$ErrorActionPreference = "Stop"
# Get the audio device matching the provided name
$device = Get-AudioDevice -List | Where-Object { $_.Name -eq $deviceName }
if ($device) {
# Set the audio device as the default
Set-AudioDevice -Index $device.Index
# Show a toast notification
New-BurntToastNotification -Text "Audio Device Switched", "Switched to $deviceName"
Write-Output "Switched to $deviceName"
} else {
New-BurntToastNotification -Text "Audio Device Switch Error", "Device $deviceName not found"
Write-Output "Device $deviceName not found"
}
}
# Get the current default audio device
$currentDevice = (Get-AudioDevice -List | Where-Object { $_.Default -eq $true }).Name
# Toggle between headphones and speakers
if ($currentDevice -eq $headphones) {
Set-DefaultAudioDevice -deviceName $speakers
} else {
Set-DefaultAudioDevice -deviceName $headphones
}
# Running the Script:
# Save this script as SwitchAudio.ps1 in your scripts folder.
# To run the script, open PowerShell and navigate to the script directory, then execute:
# .\SwitchAudio.ps1
# Creating a Shortcut:
# To create a desktop shortcut that runs this script with a double-click:
# 1. Right-click on your desktop and select "New > Shortcut".
# 2. Enter the following location for the item:
# powershell.exe -ExecutionPolicy Bypass -WindowStyle Hidden -File "C:\Users\pchin\scripts\SwitchAudio.ps1"
# 3. Name your shortcut, such as "Switch Audio".
# 4. Click "Finish".
# This setup allows you to switch between your headphone and speaker outputs quickly without manually navigating through system settings.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment