How to List Hyper-V Virtual Switches in Batch Script
Virtual switches are the networking backbone of Hyper-V. Every VM connects to a virtual switch to communicate with other VMs, the host OS, or the physical network. Listing all virtual switches is a routine administrative task for auditing network configurations, verifying switch availability before creating VMs, troubleshooting connectivity issues, and documenting infrastructure.
In this guide, we will explore how to list and inspect Hyper-V virtual switches from a Batch Script using PowerShell cmdlets.
Method 1: Basic Switch Listing
@echo off
setlocal
echo =============================================
echo HYPER-V VIRTUAL SWITCHES
echo =============================================
echo.
powershell -noprofile -command ^
"Get-VMSwitch | Format-Table Name, SwitchType, NetAdapterInterfaceDescription -AutoSize"
pause
Sample Output
Name SwitchType NetAdapterInterfaceDescription
---- ---------- ------------------------------
External-Production External Intel(R) I211 Gigabit Network Connection
Internal-Mgmt Internal
Private-Lab Private
Method 2: Detailed Switch Information
@echo off
setlocal
net session >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] Admin required.
pause
exit /b 1
)
echo Detailed Virtual Switch Report:
echo ================================
echo.
powershell -noprofile -command ^
"Get-VMSwitch | ForEach-Object { ^
Write-Host ('=== ' + $_.Name + ' ==='); ^
Write-Host (' Type: ' + $_.SwitchType); ^
Write-Host (' ID: ' + $_.Id); ^
Write-Host (' Allow Mgmt OS: ' + $_.AllowManagementOS); ^
Write-Host (' Embedded Teaming: ' + $_.EmbeddedTeamingEnabled); ^
if ($_.NetAdapterInterfaceDescription) { ^
Write-Host (' Physical Adapter: ' + $_.NetAdapterInterfaceDescription) ^
} else { ^
Write-Host (' Physical Adapter: (none - ' + $_.SwitchType + ' switch)') ^
}; ^
$switchName = $_.Name; ^
$vms = @(Get-VM | Where-Object { $_.NetworkAdapters.SwitchName -contains $switchName }); ^
Write-Host (' Connected VMs: ' + $vms.Count); ^
Write-Host '' ^
}"
pause
Method 3: Switch-to-VM Mapping
See which VMs are connected to each switch:
@echo off
setlocal
net session >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] Admin required.
pause
exit /b 1
)
echo =============================================
echo SWITCH-TO-VM MAPPING
echo =============================================
echo.
powershell -noprofile -command ^
"Get-VMSwitch | ForEach-Object { ^
$switch = $_.Name; ^
$type = $_.SwitchType; ^
Write-Host ('{0} [{1}]' -f $switch, $type) -ForegroundColor Cyan; ^
$adapters = @(Get-VMNetworkAdapter -All | Where-Object { $_.SwitchName -eq $switch -and -not $_.IsManagementOs }); ^
if ($adapters.Count -eq 0) { ^
Write-Host ' (no VMs connected)' ^
} else { ^
$adapters | ForEach-Object { ^
$state = (Get-VM -Name $_.VMName -ErrorAction SilentlyContinue).State; ^
$icon = if ($state -eq 'Running') {'*'} else {' '}; ^
Write-Host (' [{0}] {1} ({2})' -f $icon, $_.VMName, $state) ^
} ^
}; ^
Write-Host '' ^
}"
pause
Method 4: Switch Inventory with Port Statistics
For performance monitoring and capacity planning:
@echo off
setlocal
net session >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] Admin required.
pause
exit /b 1
)
echo Virtual Switch Inventory:
echo =========================
echo.
powershell -noprofile -command ^
"$switches = @(Get-VMSwitch); ^
Write-Host ('{0,-25} {1,-10} {2,-8} {3,-15} {4}' -f 'Name', 'Type', 'VMs', 'Mgmt OS', 'Physical NIC'); ^
Write-Host ('{0,-25} {1,-10} {2,-8} {3,-15} {4}' -f '----', '----', '---', '-------', '------------'); ^
$switches | ForEach-Object { ^
$name = $_.Name; ^
$vmCount = @(Get-VMNetworkAdapter -All | Where-Object { $_.SwitchName -eq $name -and -not $_.IsManagementOs }).Count; ^
$nic = if ($_.NetAdapterInterfaceDescription) { $_.NetAdapterInterfaceDescription } else { '-' }; ^
Write-Host ('{0,-25} {1,-10} {2,-8} {3,-15} {4}' -f ^
$_.Name, $_.SwitchType, $vmCount, $_.AllowManagementOS, $nic) ^
}; ^
Write-Host ''; ^
Write-Host ('Total switches: ' + $switches.Count)"
pause
Method 5: CSV Export for Documentation
@echo off
setlocal
set "output=%~dp0switch_inventory_%COMPUTERNAME%.csv"
net session >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] Admin required.
pause
exit /b 1
)
echo Exporting switch inventory to CSV...
powershell -noprofile -command ^
"Get-VMSwitch | Select-Object ^
Name, SwitchType, Id, AllowManagementOS, ^
EmbeddedTeamingEnabled, ^
NetAdapterInterfaceDescription, ^
@{Name='ConnectedVMs';Expression={ ^
$n = $_.Name; ^
(@(Get-VMNetworkAdapter -All | Where-Object { $_.SwitchName -eq $n -and -not $_.IsManagementOs }).VMName | ^
Sort-Object -Unique) -join '; ' ^
}}, ^
@{Name='VMCount';Expression={ ^
$n = $_.Name; ^
@(Get-VMNetworkAdapter -All | Where-Object { $_.SwitchName -eq $n -and -not $_.IsManagementOs }).Count ^
}} ^
| Export-Csv -Path '%output%' -NoTypeInformation"
if %errorlevel%==0 (
echo [SUCCESS] Exported to: %output%
echo.
type "%output%"
) else (
echo [ERROR] Export failed.
)
echo.
pause
Checking Available Physical Adapters
Before creating new external switches, see which physical NICs are available:
@echo off
echo Physical Network Adapters (available for external switches^):
echo ============================================================
echo.
powershell -NoProfile -Command "Get-NetAdapter -Physical | Format-Table Name, Status, LinkSpeed, InterfaceDescription, MacAddress -AutoSize"
echo.
echo Currently bound to switches:
powershell -NoProfile -Command "Get-VMSwitch -SwitchType External | ForEach-Object { Write-Host (' ' + $_.Name + ' -> ' + $_.NetAdapterInterfaceDescription) }"
pause
Common Mistakes
The Wrong Way: Using ipconfig to Find Virtual Switches
:: WRONG - ipconfig shows host network adapters, not Hyper-V switches
ipconfig /all | findstr "vEthernet"
Output Concern:
ipconfig shows the virtual network adapters created on the host for internal and external switches, but it does not show private switches (which have no host adapter) and does not show switch configuration details like type, connected VMs, or physical adapter binding. Use Get-VMSwitch for complete switch information.
The Wrong Way: Not Checking for Existing Switches Before Creating
:: WRONG - Will fail if the name already exists
powershell -command "New-VMSwitch -Name 'MySwitch' -SwitchType Internal"
Always list existing switches first with Get-VMSwitch to avoid name conflicts.
Best Practices
- Use
Get-VMSwitchfor all switch queries: It provides complete, structured information about every switch. - Map switches to VMs: Understanding which VMs use which switch is essential for network change impact analysis.
- Export regularly: CSV exports of switch configurations serve as documentation and disaster recovery references.
- Check physical adapters before creating external switches: A physical NIC can only be bound to one external switch.
- Use descriptive switch names: Include the purpose and type in the name (e.g.,
External-Production,Internal-Management).
Conclusion
Listing Hyper-V virtual switches from a Batch Script is powered by PowerShell's Get-VMSwitch cmdlet, which provides comprehensive details about each switch's type, physical adapter binding, management OS access, and connected VMs. By combining switch listings with VM-to-switch mappings, port statistics, and CSV exports, administrators maintain complete visibility into their virtual networking infrastructure. Regular switch audits ensure that network configurations remain documented, consistent, and aligned with organizational requirements.