Skip to main content

How to Connect to a Hyper-V VM Console from Batch Script

The Hyper-V VM console provides direct access to a virtual machine's display, keyboard, and mouse, similar to sitting in front of a physical machine's monitor. This is essential when a VM has no network connectivity (misconfigured NIC, broken RDP, firewall blocking), during OS installation before networking is available, or when troubleshooting boot issues that prevent the guest OS from starting fully.

In this guide, we will explore how to launch Hyper-V VM console connections from a Batch Script using the VMConnect tool and PowerShell.

Understanding VMConnect

VMConnect.exe (Virtual Machine Connection) is the built-in console viewer for Hyper-V VMs. It is located at:

%ProgramFiles%\Hyper-V\vmconnect.exe

VMConnect provides:

  • Direct console access (video, keyboard, mouse)
  • Clipboard sharing (Enhanced Session Mode)
  • Local device redirection (Enhanced Session Mode)
  • Works even when the VM has no network connectivity

Method 1: Connecting to a Local VM

@echo off
setlocal

set "vm_name=WebServer-01"

:: Verify VMConnect exists
if not exist "%ProgramFiles%\Hyper-V\vmconnect.exe" (
echo [ERROR] VMConnect not found. Install Hyper-V management tools.
pause
exit /b 1
)

echo Connecting to VM "%vm_name%"...

:: Launch VMConnect
start "" "%ProgramFiles%\Hyper-V\vmconnect.exe" localhost "%vm_name%"

echo [OK] VMConnect window opened.

Parameters

  • The first argument is the Hyper-V host (localhost for local VMs).
  • The second argument is the VM name.

Method 2: Connecting with a VM List Selection

An interactive tool that lets you choose which VM to connect to:

@echo off
title VM Console Connector
setlocal enabledelayedexpansion

:: Verify Admin
net session >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] Administrator privileges required.
pause
exit /b 1
)

if not exist "%ProgramFiles%\Hyper-V\vmconnect.exe" (
echo [ERROR] VMConnect not found.
pause
exit /b 1
)

:menu
cls
echo =============================================
echo VM CONSOLE CONNECTOR
echo =============================================
echo.

set "count=0"
for /f "usebackq delims=" %%L in (`powershell -NoProfile -Command "Get-VM | Sort-Object State, Name | ForEach-Object { $_.Name + '~' + $_.State }"`) do (
set /a count+=1
set "vm[!count!]=%%L"
for /f "tokens=1,2 delims=~" %%A in ("%%L") do (
echo !count!. %%A ^(%%B^)
)
)

if !count!==0 (
echo No VMs found.
pause
exit /b 1
)

echo.
echo 0. Exit
echo.
set /p "choice=Select VM to connect: "

if "!choice!"=="0" exit /b 0

if !choice! lss 1 (
echo [ERROR] Invalid selection.
pause
goto menu
)
if !choice! gtr !count! (
echo [ERROR] Invalid selection.
pause
goto menu
)

:: Extract VM name from stored value
set "selected=!vm[%choice%]!"
for /f "tokens=1 delims=~" %%N in ("!selected!") do set "vm_name=%%N"

echo.
echo Connecting to "!vm_name!"...
start "" "%ProgramFiles%\Hyper-V\vmconnect.exe" localhost "!vm_name!"

timeout /t 2 /nobreak >nul
goto menu

Method 3: Connecting to a Remote Hyper-V Host

VMConnect can connect to VMs on remote Hyper-V servers:

@echo off
setlocal

set "host=HyperV-Server01"
set "vm_name=RemoteVM"

if not exist "%ProgramFiles%\Hyper-V\vmconnect.exe" (
echo [ERROR] VMConnect not found. Install Hyper-V management tools.
pause
exit /b 1
)

echo Connecting to "%vm_name%" on %host%...

start "" "%ProgramFiles%\Hyper-V\vmconnect.exe" "%host%" "%vm_name%"

echo [OK] Connection initiated.
info

Remote VMConnect requires:

  • The Hyper-V management tools installed on the client.
  • The connecting user to have Hyper-V Administrator permissions on the remote host.
  • WinRM connectivity to the remote host (TCP 5985/5986).

Method 4: Starting a VM and Immediately Connecting

For development workflows where you want to start a VM and jump straight into the console:

@echo off
setlocal

set "vm_name=DevEnvironment"

net session >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] Admin required.
pause
exit /b 1
)

if not exist "%ProgramFiles%\Hyper-V\vmconnect.exe" (
echo [ERROR] VMConnect not found.
pause
exit /b 1
)

:: Check state and start if needed
set "state="
for /f "delims=" %%S in ('powershell -NoProfile -Command ^
"(Get-VM -Name '%vm_name%' -ErrorAction SilentlyContinue).State"') do set "state=%%S"

if not defined state (
echo [ERROR] VM "%vm_name%" not found.
pause
exit /b 1
)

if /i "%state%"=="Off" (
echo Starting "%vm_name%"...
powershell -NoProfile -Command ^
"try { Start-VM -Name '%vm_name%' -ErrorAction Stop }" ^
"catch { Write-Host ('[ERROR] ' + $_.Exception.Message) }"
timeout /t 3 /nobreak >nul
) else if /i "%state%"=="Saved" (
echo Resuming "%vm_name%" from saved state...
powershell -NoProfile -Command ^
"try { Start-VM -Name '%vm_name%' -ErrorAction Stop }" ^
"catch { Write-Host ('[ERROR] ' + $_.Exception.Message) }"
timeout /t 2 /nobreak >nul
) else (
echo VM is already %state%.
)

echo Opening console...
start "" "%ProgramFiles%\Hyper-V\vmconnect.exe" localhost "%vm_name%"

Method 5: Enhanced Session Mode Configuration

Enhanced Session Mode provides a richer console experience with clipboard sharing, audio redirection, and local drive mapping, similar to Remote Desktop.

@echo off
setlocal

net session >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] Admin required.
pause
exit /b 1
)

echo Current Enhanced Session Mode settings:
echo ========================================
echo.

powershell -NoProfile -Command ^
"$hi = Get-VMHost;" ^
"$status = if ($hi.EnableEnhancedSessionMode) {'Enabled'} else {'Disabled'};" ^
"Write-Host (' Host Policy: Enhanced Session Mode ' + $status);" ^
"Write-Host '';" ^
"Write-Host ' Per-VM Integration Services:';" ^
"foreach ($vm in (Get-VM | Sort-Object Name)) {" ^
" $services = @(Get-VMIntegrationService -VMName $vm.Name -ErrorAction SilentlyContinue);" ^
" $enabled = @($services | Where-Object Enabled).Count;" ^
" $total = $services.Count;" ^
" Write-Host (' {0,-25} Gen {1}, {2}/{3} services enabled' -f $vm.Name, $vm.Generation, $enabled, $total)" ^
"}"

echo.
echo [1] Enable Enhanced Session Mode (host-wide^)
echo [2] Disable Enhanced Session Mode (host-wide^)
echo [3] Exit
set /p "opt=Select: "

if "%opt%"=="1" (
powershell -NoProfile -Command ^
"try { Set-VMHost -EnableEnhancedSessionMode $true -ErrorAction Stop; exit 0 }" ^
"catch { Write-Host ('[ERROR] ' + $_.Exception.Message); exit 1 }"
if %errorlevel%==0 echo [OK] Enhanced Session Mode enabled.
) else if "%opt%"=="2" (
powershell -NoProfile -Command ^
"try { Set-VMHost -EnableEnhancedSessionMode $false -ErrorAction Stop; exit 0 }" ^
"catch { Write-Host ('[ERROR] ' + $_.Exception.Message); exit 1 }"
if %errorlevel%==0 echo [OK] Enhanced Session Mode disabled.
) else (
exit /b 0
)

pause

Opening Multiple Consoles at Once

For monitoring multiple VMs simultaneously:

@echo off
setlocal enabledelayedexpansion

set "vm_count=3"
set "vms[0]=WebServer-01"
set "vms[1]=DatabaseVM"
set "vms[2]=AppServer-01"

if not exist "%ProgramFiles%\Hyper-V\vmconnect.exe" (
echo [ERROR] VMConnect not found.
pause
exit /b 1
)

echo Opening console windows...
echo.

set /a last=vm_count - 1
for /L %%i in (0,1,!last!) do (
echo Connecting to !vms[%%i]!...
start "" "%ProgramFiles%\Hyper-V\vmconnect.exe" localhost "!vms[%%i]!"
timeout /t 1 /nobreak >nul
)

echo.
echo [OK] All console windows opened.

Using PowerShell Direct as an Alternative

For VMs with Integration Services, PowerShell Direct provides a command-line session without VMConnect:

@echo off
setlocal

set "vm_name=WebServer-01"

echo Opening PowerShell Direct session to "%vm_name%"...
echo (You will be prompted for guest credentials^)
echo.

powershell -NoProfile -NoExit -Command "Enter-PSSession -VMName '%vm_name%'"
tip

PowerShell Direct works even without network connectivity because it communicates through the VMBus (Hyper-V interconnect), not through TCP/IP. It requires Integration Services and valid guest OS credentials.

Common Mistakes

The Wrong Way: Using RDP When Networking Is Down

:: WRONG - RDP requires network connectivity
mstsc /v:192.168.1.50
:: If the VM's NIC is misconfigured, this will fail

Output Concern: When a VM's network adapter is misconfigured, disconnected, or the firewall blocks RDP, Remote Desktop cannot connect. VMConnect uses the hypervisor's virtual bus, not the network, so it always works regardless of the VM's network state.

The Wrong Way: Hardcoding the VMConnect Path

:: FRAGILE - Path may differ on some installations
"C:\Program Files\Hyper-V\vmconnect.exe" localhost "MyVM"

While the path is typically C:\Program Files\Hyper-V\, using %ProgramFiles% ensures compatibility across different Windows installations.

Best Practices

  1. Use VMConnect for console access: It works regardless of guest networking status.
  2. Enable Enhanced Session Mode: Provides clipboard sharing and device redirection for a much richer experience.
  3. Use PowerShell Direct for scripting: When you need command-line access without a GUI, PowerShell Direct is faster than VMConnect.
  4. Use %ProgramFiles%: Avoid hardcoding the VMConnect path for portability.
  5. Keep RDP for production use: VMConnect is a management tool. For daily work, Remote Desktop provides better performance and multi-monitor support.

Conclusion

Connecting to a Hyper-V VM console from a Batch Script is accomplished by launching vmconnect.exe with the host name and VM name as arguments. VMConnect provides reliable console access through the hypervisor bus, making it the go-to tool when network-based connectivity (RDP, SSH) is unavailable. By building interactive VM selectors, combining start-and-connect workflows, and enabling Enhanced Session Mode, administrators can efficiently manage VM consoles across local and remote Hyper-V hosts.