# AZ-140 Lab tips II:

## **PowerShell Script for VM Size Comparison**

This script lists all session hosts with their **host pool**, **VM name**, and **VM size** across all host pools(loop through multiple resource groups):

```powershell
Connect-AzAccount

# Get all host pools
$hostPools = Get-AzWvdHostPool

# Get all VMs in the subscription once
$allVMs = Get-AzVM

# Prepare output array
$allVMInfo = @()

foreach ($pool in $hostPools) {
    $sessionHosts = Get-AzWvdSessionHost -ResourceGroupName $pool.ResourceGroupName -HostPoolName $pool.Name

    foreach ($sessionHost in $sessionHosts) {
        $vmName = $sessionHost.Name.Split("/")[-1]

        # Search for the matching VM in all VMs
        $vm = $allVMs | Where-Object { $_.Name -eq $vmName }

        $vmSize = if ($vm) { $vm.HardwareProfile.VmSize } else { "Not Found" }
        $vmRG = if ($vm) { $vm.ResourceGroupName } else { "Unknown" }

        $allVMInfo += [PSCustomObject]@{
            HostPool     = $pool.Name
            SessionHost  = $sessionHost.Name
            VMName       = $vmName
            VMSize       = $vmSize
            ResourceGroup = $vmRG
            Status       = $sessionHost.Status
        }
    }
}

# Display the result
$allVMInfo | Format-Table

# Optional: export to CSV
$allVMInfo | Export-Csv -Path "AVD_VM_Sizes.csv" -NoTypeInformation
```
