Hypervisor Discovery
3 min
Description
Attempts to determine if a devices "Virtualization Type"
- 1 = Physical Machine (No Virtualization Detected)
- 2 = Virtual Machine Guest (Based on system/BIOS strings)
- 3 = Physical Host with Hypervisor Enabled (e.g., VBS, WSL2, but Hyper-V role not detected)
- 4 = Physical Host with Hyper-V Configured (Hyper-V role/service 'vmms' detected)
- 5 = Physical Host with Other Virtualization Software Detected (e.g., VMware Workstation/Player, VirtualBox services/processes/install found)
Frequency
- Run Daily - or on manual full pull
Powershell
#Requires -Modules CimCmdlets, Microsoft.PowerShell.Management, Microsoft.PowerShell.Utility
<#
.SYNOPSIS
Determines the virtualization status of the machine (Physical, Guest, Hyper-V Host, Other Host).
.DESCRIPTION
This script checks various system properties using WMI/CIM, services, processes,
and registry entries to classify the machine into one of five categories:
1 = Physical Machine (No Virtualization Detected)
2 = Virtual Machine Guest (Based on system/BIOS strings)
3 = Physical Host with Hypervisor Enabled (e.g., VBS, WSL2, but Hyper-V role not detected)
4 = Physical Host with Hyper-V Configured (Hyper-V role/service 'vmms' detected)
5 = Physical Host with Other Virtualization Software Detected (e.g., VMware Workstation/Player, VirtualBox services/processes/install found)
The script outputs a single integer representing the detected status.
.NOTES
Requires: PowerShell v3+, Administrator privileges to query WMI/Services/Registry comprehensively.
Run Context: Designed to run locally on the target machine.
Detection Methods: Uses HypervisorPresent flag, Hyper-V 'vmms' service check, guest VM string indicators,
and an exhaustive check for common Type 2 hypervisors (VMware Workstation/Player,
VirtualBox, Parallels) via services, processes, installed software registry keys,
and default folders.
#>
# --- Configuration ---
# Set $VerbosePreference = "Continue" to see detailed step-by-step logging
# $VerbosePreference = "SilentlyContinue" # Default
# --- Function Definitions ---
# Define a function to check for common Type 2 hypervisor indicators
function Test-OtherVirtualizationSoftware {
<#
.SYNOPSIS
Checks for indicators of common Type 2 virtualization software installation or activity.
.DESCRIPTION
Performs a reasonably exhaustive check for VMware Workstation/Player, Oracle VirtualBox,
and Parallels by looking for associated services, processes, registry entries (installed software),
and default installation folders. Returns $true if any indicator is found, $false otherwise.
.NOTES
This function checks multiple sources and may take slightly longer to execute than simple checks.
It uses ErrorAction SilentlyContinue extensively, as missing items are expected.
#>
Write-Verbose "Starting check for other virtualization software..."
# 1. Check Running Services
#--------------------------
Write-Verbose "Checking known services..."
$vmwareServiceNames = @(
"VMAuthdService", # VMware Authorization Service
"VMnetDHCP", # VMware DHCP Service
"VMUSBArbService", # VMware USB Arbitration Service
"vstor2-ws*", # VMware Storage Driver Service (Use wildcard with -Name, requires PSv6+ or filter)
# For broader compatibility, check specific known versions or rely on display name / other methods
"vmware-hostd" # Sometimes present
)
$vmwareDisplayNamesPatterns = @(
"VMware NAT Service",
"VMware Workstation Server"
# "vstor2-ws" # Check display name prefix too if needed
)
$vboxServiceNames = @(
"VBoxService", # Oracle VM VirtualBox Service
"VBoxDrv" # VirtualBox Driver Service (older?)
)
$parallelsServiceNames = @(
"Parallels Service", # Parallels Service
"Parallels Client Support", # Parallels Tools related?
"prl_disp_service", # Parallels Display Service
"prl_hostd" # Parallels Host Service
)
# Check by specific service name
foreach ($serviceName in ($vmwareServiceNames + $vboxServiceNames + $parallelsServiceNames)) {
# Handle potential wildcard for vstor2 if on PSv6+ (otherwise it might fail)
# Simpler approach: Check without wildcard first, rely on other methods if missed.
if ($serviceName -ne "vstor2-ws*" -and ($null -ne (Get-Service -Name $serviceName -ErrorAction SilentlyContinue))) {
Write-Verbose "Found service by name: $serviceName"
return $true
}
# Add specific checks for known vstor2 versions if needed, e.g.:
# if ($null -ne (Get-Service -Name "vstor2-ws60" -ErrorAction SilentlyContinue)) { return $true }
}
# Check by specific display name pattern
foreach ($pattern in $vmwareDisplayNamesPatterns) {
if ($null -ne (Get-Service -DisplayName $pattern -ErrorAction SilentlyContinue)) {
Write-Verbose "Found service by display name: $pattern"
return $true
}
}
# 2. Check Running Processes
#---------------------------
Write-Verbose "Checking known processes..."
$processNames = @(
"vmware", # Main VMware Workstation/Player UI process
"vmplayer", # VMware Player UI process
"vmware-authd", # VMware Auth service process
"vmware-tray", # VMware Tray icon
"VirtualBox", # Main VirtualBox UI/Manager process
"VBoxSVC", # VirtualBox Service process
"VBoxManage", # VirtualBox command-line tool (if running)
"prl_client_app", # Parallels Client
"prl_disp_service" # Parallels Display Service process
)
# Use try/catch as Get-Process throws terminating error if *any* name isn't found when list is passed
try {
if ($null -ne (Get-Process -Name $processNames -ErrorAction Stop)) {
Write-Verbose "Found running process from list: $($processNames -join ', ')"
return $true
}
} catch {
# Expected error if none of the processes are running
Write-Verbose "No target processes found running (or error checking: $($_.Exception.Message))."
}
# 3. Check Installed Software (Registry Uninstall Keys)
#------------------------------------------------------
Write-Verbose "Checking installed software via registry..."
$uninstallPaths = @(
'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall',
'HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall' # For 32-bit apps on 64-bit OS
)
$softwareKeywords = @(
"VMware Workstation",
"VMware Player",
"VMware VIX", # Often installed alongside Workstation
"VMware OVF Tool", # Often installed alongside Workstation
"Oracle VM VirtualBox",
#"VirtualBox Guest Additions", # Unlikely on host, commented out
"Parallels Tools", # Might be installed on Windows even if not hosting
"Parallels Desktop"
)
foreach ($path in $uninstallPaths) {
# Check if path exists before querying
if (Test-Path $path) {
# Get subkey names, then get DisplayName property from each subkey
$subkeys = Get-ChildItem -Path $path -ErrorAction SilentlyContinue
if ($subkeys) {
foreach ($key in $subkeys) {
$appName = $key.GetValue("DisplayName")
$appPublisher = $key.GetValue("Publisher") # Optional: Could check publisher too
if ($appName) { # Ensure appName is not null/empty
foreach ($keyword in $softwareKeywords) {
# Use -match for case-insensitive substring match
if ($appName -match [regex]::Escape($keyword)) {
Write-Verbose "Found installed software matching keyword '$keyword': $appName"
return $true
}
}
}
}
}
} else {
Write-Verbose "Registry path not found: $path"
}
}
# 4. Check Default Installation Folders
#--------------------------------------
Write-Verbose "Checking default installation folders..."
# Use Get-FolderPath if available (PSv6+), otherwise fallback to environment variables
$programFiles = $env:ProgramFiles
$programFilesX86 = $env:ProgramFilesX86
$foldersToCheck = @(
"$programFiles\Oracle\VirtualBox",
"$programFiles\VMware\VMware Workstation",
"$programFiles\VMware\VMware Player",
"$programFiles\Parallels\Parallels Desktop",
# Also check x86 folders if they exist and are different
"$programFilesX86\Oracle\VirtualBox",
"$programFilesX86\VMware\VMware Workstation",
"$programFilesX86\VMware\VMware Player",
"$programFilesX86\Parallels\Parallels Desktop"
) | Where-Object {$_} | Get-Unique # Filter out nulls and get unique paths
foreach ($folder in $foldersToCheck) {
if (Test-Path -Path $folder -PathType Container -ErrorAction SilentlyContinue) {
Write-Verbose "Found default installation folder: $folder"
return $true
}
}
# If none of the above checks found anything:
Write-Verbose "No indicators of other virtualization software found."
return $false
}
# Function to check for virtualization indicators in system/BIOS strings (primarily for guest detection)
function Get-VirtualizationIndicator {
param(
[Parameter(Mandatory = $true)]
$ComputerObject,
[Parameter(Mandatory = $true)]
$BiosObject
)
Write-Verbose "Checking system/BIOS strings for VM guest indicators..."
# Specific check for Hyper-V guests
if ($ComputerObject.Manufacturer -match "Microsoft Corporation" -and $ComputerObject.Model -eq "Virtual Machine") {
Write-Verbose "Detected Microsoft Virtual Machine indicator."
return $true
}
# General keywords
$virtKeywords = @("Virtual", "VMware", "VirtualBox", "Hyper-V", "KVM", "Xen", "Parallels", "QEMU")
foreach ($kw in $virtKeywords) {
# Use -match for case-insensitive substring matching
if ( ($ComputerObject.Model -match $kw) -or
($ComputerObject.Manufacturer -match $kw) -or
($BiosObject.Manufacturer -match $kw) -or
# Join BIOSVersion array elements (if it's an array) before matching
(($BiosObject.BIOSVersion -join " ") -match $kw) ) {
Write-Verbose "Found VM indicator keyword '$kw' in system/BIOS strings."
return $true
}
}
Write-Verbose "No VM guest indicators found in system/BIOS strings."
return $false
}
# --- Main Script Logic ---
# Output Codes Definition (for reference)
# 1 = Physical Machine (No Virtualization Detected)
# 2 = Virtual Machine Guest (Based on system/BIOS strings)
# 3 = Physical Host with Hypervisor Enabled (e.g., VBS, WSL2, but Hyper-V role not detected)
# 4 = Physical Host with Hyper-V Configured (Hyper-V role/service 'vmms' detected)
# 5 = Physical Host with Other Virtualization Software Detected (e.g., VMware Workstation/Player, VirtualBox services/processes/install found)
Write-Verbose "Gathering system information via WMI/CIM..."
$computer = Get-CimInstance Win32_ComputerSystem -ErrorAction SilentlyContinue
$bios = Get-CimInstance Win32_BIOS -ErrorAction SilentlyContinue
$cpu = Get-CimInstance Win32_Processor -ErrorAction SilentlyContinue
# Handle potential WMI query failures
if (-not $computer -or -not $bios -or -not $cpu) {
Write-Error "Failed to retrieve essential WMI data (Win32_ComputerSystem, Win32_BIOS, Win32_Processor). Cannot determine status."
exit 1 # Exit with non-zero status
}
Write-Verbose "Successfully gathered basic WMI data."
# Convert BIOS release date (if available) from WMI datetime format
$biosReleaseDate = "N/A"
if ($bios.ReleaseDate) {
try {
$biosReleaseDate = [System.Management.ManagementDateTimeConverter]::ToDateTime($bios.ReleaseDate)
Write-Verbose "Converted BIOS Release Date: $biosReleaseDate"
}
catch {
$biosReleaseDate = $bios.ReleaseDate # Keep original if conversion fails
Write-Warning "Failed to convert BIOS Release Date '$($bios.ReleaseDate)'."
}
} else {
Write-Verbose "BIOS Release Date not available."
}
# -- Determine overall virtualization status --
$resultInt = 0 # Default/Error state
$isVirtualIndicator = Get-VirtualizationIndicator -ComputerObject $computer -BiosObject $bios
$isOtherVirtSoftwareDetected = $false # Initialize flag
Write-Verbose "Checking HypervisorPresent flag..."
if ($computer.HypervisorPresent) {
# State 3 or 4: Windows Hypervisor Platform is running
Write-Verbose "HypervisorPresent is True. Checking for Hyper-V 'vmms' service..."
$hyperVService = Get-Service -Name vmms -ErrorAction SilentlyContinue
if ($null -ne $hyperVService) {
Write-Verbose "'vmms' service found."
$resultInt = 4 # Physical Host with Hyper-V Configured
} else {
Write-Verbose "'vmms' service not found."
$resultInt = 3 # Physical Host with Hypervisor Enabled (WHPX, VBS, WSL2 etc., but no vmms)
}
} elseif ($isVirtualIndicator) {
# State 2: Not a host based on HypervisorPresent, but looks like a guest VM based on strings
Write-Verbose "HypervisorPresent is False. Detected as VM Guest based on strings."
$resultInt = 2 # Virtual Machine Guest
} else {
# State 1 or 5: HypervisorPresent is false, and doesn't look like a guest VM.
Write-Verbose "HypervisorPresent is False and not detected as VM Guest. Checking for other virtualization software..."
# Check for other virtualization software (VMware Workstation/Player, VirtualBox, Parallels)
$isOtherVirtSoftwareDetected = Test-OtherVirtualizationSoftware
if ($isOtherVirtSoftwareDetected) {
# Found indicators of VMware Workstation/Player or VirtualBox likely installed/active
Write-Verbose "Detected other virtualization software."
$resultInt = 5 # Physical Host with Other Virtualization Software Detected
} else {
# No HypervisorPresent, not a VM guest, no other virt software detected.
Write-Verbose "No other virtualization software detected."
$resultInt = 1 # Physical Machine (No Virtualization Detected)
}
}
Write-Verbose "Final determined status code: $resultInt"
# Optionally, create a result object with details for logging or audit
$detailedResult = [PSCustomObject]@{
ComputerManufacturer = $computer.Manufacturer
ComputerModel = $computer.Model
SystemName = $computer.Name
HypervisorPresent = $computer.HypervisorPresent
SystemType = $computer.SystemType
BIOSManufacturer = $bios.Manufacturer
BIOSVersion = ($bios.BIOSVersion -join " ") # Handle potential array
BIOSReleaseDate = $biosReleaseDate
CPUVirtualizationFlag = $cpu.VirtualizationFirmwareEnabled # Check if CPU supports VT-x/AMD-V
DetectedAsVMGuest = $isVirtualIndicator
HyperVServiceFound = if ($computer.HypervisorPresent -and $resultInt -eq 4) {$true} else {$false}
OtherVirtSoftwareDetected = $isOtherVirtSoftwareDetected # Based on exhaustive checks
VirtualizationStatus = $resultInt # The final classification code
}
# --- Output ---
# Output the final integer result only
Write-Output $resultInt
# For debugging or more detailed logging, output the detailed object (comment out if not needed)
# $VerbosePreference = "Continue" # Set this at the top or use -Verbose switch
# Write-Host "----- Detailed Results -----"
# $detailedResult | Format-List *
# Write-Host "---------------------------"