-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathChangeOutputDevice.ps1
More file actions
138 lines (111 loc) · 6.8 KB
/
ChangeOutputDevice.ps1
File metadata and controls
138 lines (111 loc) · 6.8 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<#
.DESCRIPTION
https://www.powershellgallery.com/packages/AudioDeviceCmdlets/3.1.0.2
https://github.com/frgnca/AudioDeviceCmdlets
.PARAMETER SetDevice
If specified, the script will attempt to set the audio device environment variables.
.EXAMPLE
Get-AudioDevice -List | Where-Object Type -like "Playback" | Where-Object name -like "*Realtek*"
Set-AudioDevice -ID "{0.0.0.00000000}.{4229d439-1b7d-4deb-894e-544bb0fa40e1}" # speakers
Set-AudioDevice -ID "{0.0.0.00000000}.{69274eea-2c89-451d-813a-c9407258be99}" # headphones
Get-AudioDevice -List | Where-Object Type -like "Recording" | Where-Object Name -Like "*V8*" | Set-AudioDevice # set v8 mixer as default recording device
Get-AudioDevice -List | Where-Object Type -Like "Playback" | Where-Object Name -Like "*speakers*" | Set-AudioDevice # set speakers as default playback device
.NOTES
Requires the AudioDeviceCmdlets module
Install-Module -Name AudioDeviceCmdlets -Scope CurrentUser
To set custom device names, set the following user environment variables:
[System.Environment]::SetEnvironmentVariable("HEADPHONES_DEVICE_NAME", "Headphones", "Machine")
[System.Environment]::SetEnvironmentVariable("SPEAKERS_DEVICE_NAME", "Output Monitor", "Machine")
[System.Environment]::SetEnvironmentVariable("SOUNDCARD_DEVICE_NAME", "Output Mixer", "Machine")
or this :
$env:SPEAKERS_DEVICE_NAME = "Output Front Panel"
$env:HEADPHONES_DEVICE_NAME = "Headphones"
$env:SOUNDCARD_DEVICE_NAME = "Output Mixer"
#>
param (
[switch]$SetDevice
)
$HeadphonesDeviceName = "*Headphones*"
$SpeakersDeviceName = "*Output Front Panel*"
$SoundcardDeviceName = "*Output Mixer*"
if ($SetDevice) {
Write-Host "`nAvailable Playback Audio Devices :"
Get-AudioDevice -List | Where-Object { $_.Type -eq "Playback" } | Select-Object Index, Default, DefaultCommunication, Name | Format-Table -AutoSize
Write-Host "`nCurrent Value: "
Write-Host "HeadphonesDeviceName = $HeadphonesDeviceName" -ForegroundColor Green
Write-Host "SpeakersDeviceName = $SpeakersDeviceName" -ForegroundColor Green
Write-Host "SoundcardDeviceName = $SoundcardDeviceName" -ForegroundColor Green
Write-Host "`nRegex Patern (Type the whole line to the prompt): "
Write-Host '^\$HeadphonesDeviceName\s*=.*' -ForegroundColor Red
Write-Host '^\$SpeakersDeviceName\s*=.*' -ForegroundColor Red
Write-Host '^\$SoundcardDeviceName\s*=.*' -ForegroundColor Red
Write-Host 'Enter Regex Pattern to find what variable to change : ' -NoNewline
$regexPatern = Read-Host
Write-Host "`nPossible new variable value (Type the whole line to the prompt): "
Write-Host '$HeadphonesDeviceName = "*Headphones*"' -ForegroundColor Red
Write-Host '$SpeakersDeviceName = "*Output Front Panel*"' -ForegroundColor Red
Write-Host '$SoundcardDeviceName = "*Output Mixer*"' -ForegroundColor Red
Write-Host '$MonitorDeviceName = "*Output Monitor*"' -ForegroundColor Red
Write-Host "`nSet variable value : " -NoNewline
$newValue = Read-Host
$scriptsDir = Split-Path -Parent $MyInvocation.MyCommand.Definition
Set-Location -Path $scriptsDir
$scriptPath = "$(Get-Location)\ChangeOutputDevice.ps1"
$content = Get-Content -Path $scriptPath
# The regex '^\$TargetVar\s*=.*' finds the line starting with $TargetVar = [anything]
$modifiedContent = $content -replace $regexPatern, $newValue
Set-Content -Path $scriptPath -Value $modifiedContent
return
}
function WindowsNotificationBalloon($text) {
# windows 10 notification balloon
Add-Type -AssemblyName System.Windows.Forms
$global:BalloonNotification = New-Object System.Windows.Forms.NotifyIcon
$path = (Get-Process -id $pid).Path
$BalloonNotification.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($path)
$BalloonNotification.BalloonTipIcon = [System.Windows.Forms.ToolTipIcon]::Info
$BalloonNotification.BalloonTipText = "${text}"
$BalloonNotification.BalloonTipTitle = "Change Output Device"
$BalloonNotification.Visible = $true
$BalloonNotification.ShowBalloonTip(5000)
}
# if AudioDeviceCmdlets module is not installed, prompt to install it
if (!(Get-Module -ListAvailable -Name AudioDeviceCmdlets -ErrorAction SilentlyContinue)) {
Write-Host "`nAudioDeviceCmdlets module is not installed." -ForegroundColor Red
Write-Host "Install AudioDeviceCmdlets module ? [Y/n] " -NoNewline -ForegroundColor Yellow
$response = Read-Host
if ($response -eq "Y" -or $response -eq "y" -or $response -eq "") {
Install-Module -Name AudioDeviceCmdlets -Scope CurrentUser
} else {
Write-Host "Exiting script."
}
return
}
#!########################################################################################################
#! Check and Change Output Device #
#!########################################################################################################
# if headphones is the default output then change it to speakers
if (Get-AudioDevice -PlaybackCommunication | Where-Object { $_.Type -eq "Playback" -and $_.Name -like $HeadphonesDeviceName }) {
Write-Host "Change default audio device to Speakers."
Get-AudioDevice -List | Where-Object { $_.Type -eq "Playback" -and $_.Name -like $SpeakersDeviceName } | Set-AudioDevice
WindowsNotificationBalloon "Change default audio device to Speakers."
}
# if speakers is default audio then change it to headphones
elseif (Get-AudioDevice -PlaybackCommunication | Where-Object { ($_.Type -eq "Playback" -and $_.Name -like $SpeakersDeviceName) }) {
# if there's no headphone device output then change it to soundcard output
if (!(Get-AudioDevice -List | Where-Object { $_.Type -eq "Playback" -and $_.Name -like $HeadphonesDeviceName })) {
Write-Host "Change default audio device to Output Mixer."
Get-AudioDevice -List | Where-Object { $_.Type -eq "Playback" -and $_.Name -like $SoundcardDeviceName } | Set-AudioDevice
WindowsNotificationBalloon "Change default audio device to Output Mixer."
return
}
Write-Host "Change default audio device to Headphones."
Get-AudioDevice -List | Where-Object { $_.Type -eq "Playback" -and $_.Name -like $HeadphonesDeviceName } | Set-AudioDevice
WindowsNotificationBalloon "Change default audio device to Headphones."
}
# if soundcard is default audio then change it to speakers
elseif (Get-AudioDevice -PlaybackCommunication | Where-Object { $_.Type -eq "Playback" -and $_.Name -like $SoundcardDeviceName }) {
Write-Host "Change default audio device to Speakers."
Get-AudioDevice -List | Where-Object { $_.Type -eq "Playback" -and $_.Name -like $SpeakersDeviceName } | Set-AudioDevice
WindowsNotificationBalloon "Change default audio device to Speakers."
}