Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions EXAMPLE/Cycle-Audio-Devices.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# This script cycles through all audio devices


# Get a list of all enabled devices
[System.Collections.ArrayList]$audioDevicesList = Get-AudioDevice -List

# Get the default playback device as <AudioDevice>
$defaultPlaybackDevice = Get-AudioDevice -Playback

#Remove non-playback devices from audioDevicesList
for ( $index = 0; $index -lt $audioDevicesList.count; $index++ ) {
if ($audioDevicesList[$index].type -ne "Playback") {
$audioDevicesList.Remove($audioDevicesList[$index])
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing the number of item in an array while iterating through it this way using a for can end up not producing the desired result. I would instead suggest something like
Get-AudioDevice -List | Where-Object -Property Type -EQ "Playback"

}
}

#Loop through audioDevicesList
for ( $index = 0; $index -lt $audioDevicesList.count; $index++ ) {
#Find the position of the default audio device
if ( $audioDevicesList[$index].name -eq $defaultPlaybackDevice.name) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fact that different devices can have the same name can cause this line to not work as it should. I would suggest something like replacing .name by .ID

#If it's the last device set the first audio device as the default
if ( $index -eq $audioDevicesList.count - 1) {
Set-AudioDevice -ID $audioDevicesList[0].id
}
#Otherwhise set the next audio device on the list as default
else {
Set-AudioDevice -ID $audioDevicesList[$index + 1].id
}
}
}