Search code examples
arraylistindexingpowershell-3.0

display the 1st variable in a tuple in PowerShell


I want to set the Netadapter "Roaming aggressiveness" value to "lowest" on all of our computers.

The wifi chips vary from one computer to another so that "Roaming aggressiveness" lowest value on on computer may display as "1.Lowest" vs another that displays as "1. Lowest" vs another with "1 - Lowest" and who knows what other variations.

Because of this, the below script does not work for ALL of our computers when trying to set them all to lowest roaming aggressiveness.

Set-NetadapterAdvancedProperty -Name "Wi-Fi" -DisplayName "Roaming aggressiveness" -DisplayValue "1.Lowest"

To get around this, I want to replace the "1.Lowest" instead to a variable that references the 1st DisplayValue without needing to know the actual name. I just know the 1st item is always the lowest option.

I am able to get the ValidDisplayValues into a variable called $RoamOptions:

$RoamOptions = Get-NetAdapterAdvancedProperty Wi-Fi -DisplayName *Roam* | ft ValidDisplayValues

The result of this on a sample computer is

ValidDisplayValues
------------------
{1. Lowest, 2. Medium-low, 3. Medium, 4. Medium-High...}

How do I create a variable $LowestOption that grabs the 1st item in the list so I can then reference it as follows so it would work for all of our computers?

Set-NetadapterAdvancedProperty -Name "Wi-Fi" -DisplayName "Roaming aggressiveness" -DisplayValue $LowestOption

Hope that makes sense.

Thank you


Solution

  • You need to get rid of the format-table and use this syntax.

    PS> $x = (Get-NetAdapterAdvancedProperty "*Wi-Fi" -DisplayName *Roam*).ValidDisplayValues
    
    PS> $x.count
    5
    
    PS> $x[0]
    1. Lowest
    
    PS> 
    

    Note: the * in front of Wi-Fi is because mine has been renamed. Might be good to use incase yours is likewise.