Just getting the 'value' of get-vm without headers

I am getting the VM’s from an ESXi host and displaying them, formatted . Then I want to search that list with a user entered VM and verify against that list that the VM entered exists. I have this (note this is only partial code of the script):

Write-Host "Displaying VM's on host: " $vchost -ForegroundColor Green
$script:VCHostVMs = get-vmhost -name $vchost | get-vm | Select Name,@{N='GuestOS';E={$_.GuestID}} | ft
$script:VMvalue = get-vmhost -name $vchost | get-vm | Select Name -ExpandProperty Name
$cnt = [pscustomobject]@{ VMCount = (Get-VMHost -Name $vchost | Get-VM).Count }
$fcount = $cnt.vmcount

The only way I could figure to show a formatted output so the user could see and then enter an existing name to run a test against a list of vm’s was do get-vm a second time with a second variable and Name -expandproperty. Is there a cleaner way? Not asking you to write my code, just asking if there is a better way? It’s the “cake and eat it too” scenario.



write-host "Total number running VMs= " -NoNewline






You actually call Get-VMHost | Get-VM 3 times. If you capture all possible output in one go, then you can pull what you want out of it without repeating the calls.

Write-Host "Displaying VM's on host: " $vchost -ForegroundColor Green

$GetAllOutputInOneCommand = get-vmhost -name $vchost | get-vm | Select-Object -Property *

$script:VCHostVMs = $GetAllOutputInOneCommand | Select-Object Name,@{N='GuestOS';E={$_.GuestID}}
$script:VMvalue = $GetAllOutputInOneCommand | Select-Object -ExpandProperty Name
$cnt = [pscustomobject]@{ VMCount = $GetAllOutputInOneCommand.Count }
$fcount = $cnt.vmcount