-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAudioDeviceChanger.ps1
More file actions
51 lines (43 loc) · 2.12 KB
/
AudioDeviceChanger.ps1
File metadata and controls
51 lines (43 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# your audio devices in windows have to match the values set in $AudioDevice_1 and $AudioDevice_2
# rename your audio devices under Windows accordingly
# go to: Settings -> Sound -> Output
# audio device names
$AudioDevice_1 = "Headphones"
$AudioDevice_2 = "Speakers"
# path to nircmd.exe
$NirCmd = "$PSScriptRoot\nircmd-x64\nircmd.exe"
# arguments for "nircmd.exe" (reference https://nircmd.nirsoft.net/setdefaultsounddevice.html)
# arguments for audio device 1
$Args_AudioDevice_1_Multimedia = "setdefaultsounddevice `"$AudioDevice_1`" 1" # Multimedia
$Args_AudioDevice_1_Communications = "setdefaultsounddevice `"$AudioDevice_1`" 2" # Communications
# arguments for audio device 2
$Args_AudioDevice_2_Multimedia = "setdefaultsounddevice `"$AudioDevice_2`" 1" # Multimedia
$Args_AudioDevice_2_Communications = "setdefaultsounddevice `"$AudioDevice_2`" 2" # Communications
# custom registry key for the active audio device setting
$RegKey = "HKCU:\Software\AudioDeviceSetting"
$RegName = "ActiveDevice"
$RegValue = "$AudioDevice_1"
# create registry key if not present
if ( -not (Test-Path -Path $RegKey) )
{
New-Item -Path $RegKey
New-ItemProperty -Path $RegKey -Name $RegName -Value $RegValue
}
# get the active audio device from the registry and switch accordingly
$ActiveAudioDevice = Get-ItemProperty -Path $RegKey -Name $RegName
Write-Host "$($ActiveAudioDevice.$RegName)"
Switch ( $($ActiveAudioDevice.$RegName) )
{
# switch from audio device 1 to audio device 2 and update the registry key
$AudioDevice_1 {
Start-Process -FilePath $NirCmd -ArgumentList $Args_AudioDevice_2_Multimedia
Start-Process -FilePath $NirCmd -ArgumentList $Args_AudioDevice_2_Communications
New-ItemProperty -Path $RegKey -Name $RegName -Value $AudioDevice_2 -Force | Out-Null
}
# switch from audio device 2 to audio device 1 and update the registry key
$AudioDevice_2 {
Start-Process -FilePath $NirCmd -ArgumentList $Args_AudioDevice_1_Multimedia
Start-Process -FilePath $NirCmd -ArgumentList $Args_AudioDevice_1_Communications
New-ItemProperty -Path $RegKey -Name $RegName -Value $AudioDevice_1 -Force | Out-Null
}
}