Array not sorting correctly

Hello folks,

I am having a hard time properly sorting the values stored in an array. Pardon if I am not accurately using the right terminology. I am not a developer but just someone who is passionate about efficiency.

The $output variable is storing all the networks available for an ESX host. These are being displayed as a menu with number values assigned to each network. See code and output below.

#Getting network info from host
$output = Get-VMHost HOST | Get-VirtualPortGroup | Select -ExpandProperty Name

#Convert output into array
$network = $output -split “`n” | Sort-Object

#Display the array as a menu
Write-Host “Networks Available” Write-Host “---------” for ($i = 12; $i -le $network.Count; $i++) { $NetworkNumber = $i + 1 Write-Host “$NetworkNumber. $($network[$i])” }

#Menu output from the $output array Networks Available

  1. TEST_10.10.10.0
  2. DEV_10.11.10.0
  3. PRD_10.12.10.0

#Prompt user for network selection** `$vmSubnet = Read-Host -Prompt “Network Name”
Network Name:

From the output above, I have a prompt that prompts a user for an input. The input is expecting a numeric value such as 1-3. Example, 1 should be TEST_10.10.10.0 and 2 would be DEV_10.11.10.0.

The issue that I can’t get around is that the number being assigned to each network label inside the array does not match the network once I try to apply the array as a value. See below.

#Applying portgroup values `Get-VM -name VM01 | Get-NetworkAdapter | Set-NetworkAdapter -NetworkName $network[$vmSubnet] -StartConnected $true -confirm:$false

What ends up happening is the VM will be updated and a network will be assigned but not the one that corresponds to the number I chose. It is randomly assigned and does not match the original $output variable. What am I missing here?


Welcome back to the forum. :wave:t3: … long time no see.

Before we proceed … When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in 1 <---- Click :point_up_2:t4: :wink:

( !! Sometimes the preformatted text button hides behind the settings gear symbol. :wink: )

Now … your issue …

I’d see three possible solutions for your issue

  1. Make the index part of the array. You could use a [PSCustomObject] for that.
  2. Use an Out-GridView to prompt for the user choice.
  3. Use what others already prepared for such cases … read on!

Time ago I had to solve another challenge but the result could help you in this case as well.
I had to prompt for choice for a large amount of items and didn’t like the way how PowerShell displayed it. Either you had to scroll a long list or - if you use Format-Wide to use the whole width of the screen the list would be sorted weirdly.
So I came up with a function to circumvent this … take a look and use it if you like it:

Hello Olaf,

Thanks for the constructive feedback. I am not too familiar with the PSCustomObject. Would you mind sharing an example of how to apply it with the code I provided? Thanks!

Have you tried the function I linked?