0

Hi I'm trying to get a menu listing of all VMs in Hyper-V Host. However I'm getting extra information in my menu list. How can I get rid of it?

'''

  $VMs = @(Get-VM | Select-Object Name)
    $menuIndex = 1
    Write-Host "Select a VM from the list"
    $VMs | foreach {
        Write-Host $menuIndex " - $_";
        $menuIndex += 1;
        }

     $ChosenItem = [int](Read-Host "Please select a Virtual Machine (1 to $($menuIndex-1))")

$VM = $VMs[$ChosenItem-1]

'''

The output of this script gives me extra data I don't want to display. I just need the VM Name.

Instead I get:

1 - @{Name=VirtualMachine1} 2 - @{Name=VirtualMachine2}

I would desire to get the following instead:

1 - VirtualMachine1 2 - VirtulaMachine2

How do I remove the "@{Name=}" portion of the output?

Thank You

  • 2
    Try `Select-Object -ExpandProperty Name` – Theo Apr 22 '20 at 17:33
  • Why are you even defining the array like that? $VMs = Get-VM | Select-Object Name will work just fine. – Scepticalist Apr 22 '20 at 17:47
  • In short: [`Select-Object`](https://learn.microsoft.com/powershell/module/microsoft.powershell.utility/select-object) (`select`) returns _a `[pscustomobject]` instance_ that has the _requested properties_ - even when you're only asking for a _single_ property. To get only that property's _value_, use `-ExpandProperty ` instead - see the [linked answer](https://stackoverflow.com/a/48809321/45375) for details and alternatives. – mklement0 Apr 22 '20 at 17:55

0 Answers0