frgnca / AudioDeviceCmdlets

AudioDeviceCmdlets is a suite of PowerShell Cmdlets to control audio devices on Windows
MIT License
772 stars 92 forks source link

Retrieve audio adapter and endpoint device names separately #80

Open upthekhyber opened 2 months ago

upthekhyber commented 2 months ago

When running Get-AudioDevice -List, the returned device name strings are a combination of both the audio adapter and the endpoint device names, formatted as "Endpoint device name (Audio adapter name)".

Is it possible to retrieve both names separately/independently (I'm actually only interested in the audio adapter name)?

mefranklin6 commented 2 months ago

That's obtainable using built-in PowerShell methods for sting manipulation. Hopefully this helps:

# Load the "Name" results from the query into a list
$audio_list = Get-AudioDevice -List | Select-Object -ExpandProperty "Name"

# iterate over the list for each result from the query
foreach ($line in $audio_list) {
    # Find the first space followed by an open parenthesis
    $audio_device = $line -Split " \("
    # Only keep what's after the first space followed by open parenthesis, which is the adapter name
    $audio_device = $audio_device[-1]
    # Print the result without the last character, which is the closed parenthesis
    Write-Output $audio_device.Substring(0, $audio_device.Length -1)
}
upthekhyber commented 2 months ago

That's obtainable using built-in PowerShell methods for sting manipulation.

Unfortunately, this is not reliable because the endpoint device name itself can contain parentheses.

mefranklin6 commented 2 months ago

only if there's a space before the parenthesis, which I haven't come across. Usually it's something like "Intel(R)"

This may be more robust though as it can handle a string with multiple "space then open parenthesis" occurrences in it.

$audio_list = Get-AudioDevice -List | Select-Object -ExpandProperty "Name"

foreach ($line in $audio_list) {
    $audio_device_fragments = $line -split " \("

    $audio_device = $null
    if ($audio_device_fragments.Length -eq 2) {
        $audio_device = $audio_device_fragments[-1].TrimEnd(')')
    }
    elseif ($audio_device_fragments.Length -ge 3) {
        $audio_device = ($audio_device_fragments[1..($audio_device_fragments.Length - 1)] -join '(').TrimEnd(')')
    }

    Write-Output $audio_device
}

Also I'm not saying it's not a valid feature request, just trying to give back to this repo that I use extensively and help out where I can :)