-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathChangeOutputDevice.ps1
More file actions
135 lines (104 loc) · 5.5 KB
/
Copy pathChangeOutputDevice.ps1
File metadata and controls
135 lines (104 loc) · 5.5 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
<#
.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
Edit `$OutputDevices below to set the playback devices and their cycle order.
#>
param (
[switch]$SetDevice
)
$OutputDevices = @("*Output Monitor*", "*Headphones*", "*Output Mixer*")
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)
}
function checkAudioDeviceCmdlets() {
# 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
}
}
if ($SetDevice) {
checkAudioDeviceCmdlets
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 "OutputDevices = @($($OutputDevices -join ', '))" -ForegroundColor Green
Write-Host "`nRegex Pattern (Type the whole line to the prompt) : "
Write-Host '^\$OutputDevices\s*=.*' -ForegroundColor Red
Write-Host "`nEnter Regex Pattern to find what variable to change : " -NoNewline
$regexPattern = Read-Host
Write-Host "`n`n`nPossible new variable value (Type the whole line to the prompt) : "
Write-Host '$OutputDevices = @("*Output Monitor*", "*Headphones*", "*Output Mixer*")' -ForegroundColor Red
Write-Host "`nSet variable value : " -NoNewline
$newValue = Read-Host
if ($null -eq $regexPattern -or $regexPattern -eq "") {
return Write-Host "`nNo regex pattern entered. Exiting script."
}
if ($null -eq $newValue -or $newValue -eq "") {
return Write-Host "`nNo new value entered. Exiting script."
}
$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 $regexPattern, $newValue
Set-Content -Path $scriptPath -Value $modifiedContent
return
}
#!########################################################################################################
#! Check and Change Output Device #
#!########################################################################################################
checkAudioDeviceCmdlets
$playbackDevices = @(Get-AudioDevice -List | Where-Object { $_.Type -eq "Playback" })
$availableOutputDevices = @(
foreach ($devicePattern in $OutputDevices) {
$playbackDevices | Where-Object { $_.Name -like $devicePattern } | Select-Object -First 1
}
)
if ($availableOutputDevices.Count -eq 0) {
Write-Host "No configured output devices are currently available." -ForegroundColor Yellow
return
}
$currentDevice = Get-AudioDevice -PlaybackCommunication
$currentIndex = -1
for ($index = 0; $index -lt $availableOutputDevices.Count; $index++) {
if ($availableOutputDevices[$index].ID -eq $currentDevice.ID) {
$currentIndex = $index
break
}
}
# An unconfigured current device starts the cycle at its first available entry.
$nextIndex = if ($currentIndex -lt 0) { 0 } else { ($currentIndex + 1) % $availableOutputDevices.Count }
$nextDevice = $availableOutputDevices[$nextIndex]
Write-Host "Change default audio device to $($nextDevice.Name)."
$nextDevice | Set-AudioDevice
WindowsNotificationBalloon "Change default audio device to $($nextDevice.Name)."