Here's a quick way to tell whether a given machine is real or not: check the Win32_Baseboard class. You can use either Get-WmiObject or Get-CimInstance. Notice the results from a few physical machines.
Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
Now see the result when querying a Hyper-V virtual machine:
I don't have any VMware available so I don't know what kind of result that would show. I also haven't done extensive testing with items like a Microsoft Surface. I threw together this simple function you could use.
#requires -version 4.0 Function Test-IsVM { [cmdletbinding()] Param( [Parameter( ValueFromPipeline, ValueFromPipelineByPropertyName, HelpMessage = "Enter the name of a computer")] [ValidateNotNullorEmpty()] [string]$Computername = $env:COMPUTERNAME ) Begin { Write-Verbose "Starting: $($MyInvocation.Mycommand)" $paramHash = @{ ClassName = 'Win32_Baseboard' ErrorAction = 'Stop' } } #begin Process { Write-Verbose "Querying $computername" Try { $paramHash.ComputerName = $Computername $data = Get-CimInstance @paramHash if ($data.Product -match "virtual") { $True } else { $False } } Catch { Write-Warning "$($computername.ToUpper()) : $($_.Exception.message)" } } #process End { Write-Verbose "Ending: $($MyInvocation.Mycommand)" } #end } #end function
Have fun.
Just tried it on a VMWare box and got this:
Product : 440BX Desktop Reference Platform
Thanks for the info. Is this from something running under VMware Workstation or a Vsphere server? In either case, it wouldn’t be hard to adjust the function to account for that product.
My VMs (under vSphere) are returning also 440BX Desktop Reference Platform
Thanks for testing. That is easy enough to test for with a regular expression.
You can see how to detect other people here: http://social.technet.microsoft.com/wiki/contents/articles/942.hyper-v-how-to-detect-if-a-computer-is-a-vm-using-script.aspx
Checking the Model in the Win32_ComputerSystem class has always worked well for me. Good news is: Both are populated even back to Windows Server 2003 for those who still have to code for it.
Yep, that works as well.
You can also get this information from the registry (in case of Hyper-V, don’t know about VM ware or other environments).
$IsVM = Test-Path “HKLM:\SOFTWARE\Microsoft\Virtual Machine\Guest\Parameters”
If($IsVM)
{
$VMHost = (Get-Item “HKLM:\SOFTWARE\Microsoft\Virtual Machine\Guest\Parameters”).GetValue(“HostName”)
}