How to Get the IP Address of a Hyper-V VM in Batch Script
Knowing the IP address of a virtual machine is essential for connecting via Remote Desktop, SSH, web browsers, or any network-based management tool. While you could log into the VM's console and check manually, Hyper-V provides mechanisms to query the guest's network configuration directly from the host through Integration Services, eliminating the need to interact with the guest OS.
In this guide, we will explore how to retrieve the IP addresses of Hyper-V virtual machines from a Batch Script using PowerShell and the Hyper-V management cmdlets.
Prerequisites
For IP address retrieval to work, the guest VM must:
- Be in the Running state.
- Have Hyper-V Integration Services installed and active (specifically the "Data Exchange" / KVP component).
- Have a network adapter connected to a virtual switch with a DHCP server or a statically configured IP.
Without Integration Services, Hyper-V cannot query the guest's network configuration from the host side.
Method 1: Getting the IP Address of a Specific VM
@echo off
setlocal
set "vm_name=WebServer-01"
:: Verify Admin
net session >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] Administrator privileges required.
pause
exit /b 1
)
echo IP addresses for "%vm_name%":
echo.
powershell -NoProfile -Command ^
"$vm = Get-VM -Name '%vm_name%' -ErrorAction SilentlyContinue;" ^
"if (-not $vm) { Write-Host '[ERROR] VM not found.'; exit 1 };" ^
"if ($vm.State -ne 'Running') { Write-Host '[ERROR] VM is not running.'; exit 1 };" ^
"foreach ($nic in $vm.NetworkAdapters) {" ^
" Write-Host (' Adapter: ' + $nic.Name + ' (Switch: ' + $nic.SwitchName + ')');" ^
" if ($nic.IPAddresses.Count -gt 0) {" ^
" foreach ($addr in $nic.IPAddresses) { Write-Host (' IP: ' + $addr) }" ^
" } else {" ^
" Write-Host ' IP: (not available - check Integration Services)'" ^
" }" ^
"}"
pause
Sample Output
Adapter: Network Adapter (Switch: External-Production)
IP: 192.168.1.50
IP: fe80::1a2b:3c4d:5e6f:7890
The output typically includes both IPv4 and IPv6 addresses.
Method 2: Getting Only the IPv4 Address
To filter out IPv6 and link-local addresses:
@echo off
setlocal
set "vm_name=WebServer-01"
net session >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] Admin required.
pause
exit /b 1
)
:: Get only IPv4 addresses
set "ip="
for /f "delims=" %%I in ('powershell -NoProfile -Command ^
"(Get-VM -Name '%vm_name%' -ErrorAction SilentlyContinue).NetworkAdapters.IPAddresses | Where-Object { $_ -match '^\d+\.\d+\.\d+\.\d+$' } | Select-Object -First 1"') do (
set "ip=%%I"
)
if defined ip (
echo %vm_name%: %ip%
) else (
echo [ERROR] No IPv4 address found for %vm_name%.
)
pause
Method 3: Listing IP Addresses of All VMs
For a complete network inventory of all running VMs:
@echo off
setlocal
net session >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] Admin required.
pause
exit /b 1
)
echo =============================================
echo HYPER-V VM IP ADDRESS INVENTORY
echo =============================================
echo.
powershell -NoProfile -Command ^
"Get-VM | Where-Object State -eq 'Running' | Sort-Object Name | ForEach-Object {" ^
" $name = $_.Name;" ^
" $ips = @($_.NetworkAdapters.IPAddresses | Where-Object { $_ -match '^\d+\.\d+\.\d+\.\d+$' });" ^
" if ($ips.Count -gt 0) {" ^
" $ipList = $ips -join ', ';" ^
" Write-Host ('{0,-25} {1}' -f $name, $ipList)" ^
" } else {" ^
" Write-Host ('{0,-25} (no IPv4 address)' -f $name)" ^
" }" ^
"}"
echo.
pause
Sample Output
DomainController 192.168.1.10
WebServer-01 192.168.1.50
WebServer-02 192.168.1.51
SQLServer-01 192.168.1.60
DevEnvironment 10.0.0.5
Method 4: CSV Export with IP Addresses
@echo off
setlocal
set "output=%~dp0vm_ips_%COMPUTERNAME%.csv"
net session >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] Admin required.
pause
exit /b 1
)
echo Exporting VM IP addresses to CSV...
powershell -NoProfile -Command ^
"Get-VM | Where-Object State -eq 'Running' | ForEach-Object {" ^
" $vmName = $_.Name;" ^
" foreach ($nic in $_.NetworkAdapters) {" ^
" $v4 = ($nic.IPAddresses | Where-Object { $_ -match '^\d+\.\d+\.\d+\.\d+$' }) -join '; ';" ^
" $v6 = ($nic.IPAddresses | Where-Object { $_ -match ':' }) -join '; ';" ^
" [PSCustomObject]@{" ^
" VMName = $vmName;" ^
" Switch = $nic.SwitchName;" ^
" IPv4 = $v4;" ^
" IPv6 = $v6;" ^
" MACAddress = $nic.MacAddress" ^
" }" ^
" }" ^
"} | Export-Csv -Path '%output%' -NoTypeInformation"
if %errorlevel%==0 (
echo [SUCCESS] Exported to: %output%
echo.
type "%output%"
) else (
echo [ERROR] Export failed.
)
echo.
pause
Method 5: Wait for a VM to Get an IP Address
After starting a VM, it takes a few seconds for DHCP assignment. This script waits until an IP appears.
@echo off
setlocal enabledelayedexpansion
set "vm_name=NewVM"
set "max_wait=60"
set "interval=3"
net session >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] Admin required.
pause
exit /b 1
)
echo Waiting for "%vm_name%" to acquire an IP address...
set "ip="
set /a waited=0
:wait_ip
for /f "delims=" %%I in ('powershell -NoProfile -Command ^
"@(Get-VM -Name '%vm_name%' -ErrorAction SilentlyContinue).ForEach({ $_.NetworkAdapters.IPAddresses }) | Where-Object { $_ -match '^\d+\.\d+\.\d+\.\d+$' } | Select-Object -First 1"') do (
set "ip=%%I"
)
if defined ip (
echo.
echo [SUCCESS] %vm_name% has IP: !ip! (took ~!waited!s^)
pause
exit /b 0
)
set /a waited+=interval
if !waited! geq %max_wait% (
echo.
echo [TIMEOUT] No IP address after %max_wait% seconds.
echo Check VM networking and DHCP configuration.
pause
exit /b 1
)
echo Waiting... (!waited!s / %max_wait%s^)
timeout /t %interval% /nobreak >nul
goto wait_ip
Using IP Addresses in Automation
Once you have the IP, you can use it for automated management tasks:
@echo off
setlocal
set "vm_name=WebServer-01"
:: Get the VM's IPv4 address
set "ip="
for /f "delims=" %%I in ('powershell -NoProfile -Command ^
"(Get-VM -Name '%vm_name%' -ErrorAction SilentlyContinue).NetworkAdapters.IPAddresses | Where-Object { $_ -match '^\d+\.\d+\.\d+\.\d+$' } | Select-Object -First 1"') do (
set "ip=%%I"
)
if not defined ip (
echo [ERROR] Cannot get IP for %vm_name%.
pause
exit /b 1
)
echo %vm_name% IP: %ip%
echo.
:: Example: Ping test
echo Pinging %ip%...
ping -n 3 %ip%
:: Example: Open Remote Desktop
echo.
set /p "rdp=Open Remote Desktop to %ip%? (Y/N): "
if /i "%rdp%"=="Y" mstsc /v:%ip%
pause
Common Mistakes
The Wrong Way: Assuming All VMs Report IP Addresses
:: WRONG assumption - Returns nothing if Integration Services are missing
powershell -Command "(Get-VM -Name 'LinuxVM').NetworkAdapters.IPAddresses"
Output Concern:
If the guest VM does not have Hyper-V Integration Services installed (common on minimal Linux installations), the IPAddresses property will be empty. For Linux VMs, install hyperv-daemons or hv_utils to enable KVP data exchange.
The Wrong Way: Querying Stopped VMs
:: WRONG - A stopped VM has no active network configuration
powershell -Command "(Get-VM -Name 'OfflineVM').NetworkAdapters.IPAddresses"
Stopped VMs do not have network adapters active, so no IP address is available. Always check that the VM state is Running before querying network information.
Best Practices
- Filter for IPv4: Guest VMs report both IPv4 and IPv6 addresses. Filter with a regex for most use cases.
- Verify Integration Services: Ensure the KVP (Key-Value Pair) data exchange service is running in the guest for IP reporting.
- Wait after startup: Allow 10-30 seconds after starting a VM for DHCP assignment before querying the IP.
- Handle multiple NICs: VMs with multiple network adapters will have multiple IP addresses. Query per-adapter for clarity.
- Use for automation: Integrate IP retrieval into deployment, monitoring, and management scripts to eliminate hardcoded addresses.
Conclusion
Retrieving the IP address of a Hyper-V virtual machine from a Batch Script is accomplished by querying the NetworkAdapters.IPAddresses property through PowerShell's Get-VM cmdlet. This host-side query leverages Hyper-V Integration Services to read the guest's network configuration without logging into the VM. By filtering for IPv4 addresses, building network inventories, and waiting for DHCP assignment after startup, administrators can create fully automated workflows that dynamically discover and connect to their virtual machines.