Setting Guest IP Addresses From The Host

Last week at TechEd I had several people ask about how they can set the IP address of a guest from the host.  We've had this functionality in Hyper-V since Windows Server 2012, it was largely introduced for Hyper-V replication (to allow you to set a different IP address on failover).  But with a few lines of WMI you can take advantage of this functionality as well.  Here's how.


$vmName = "Demo"

#Get an instance of the management service, the Msvm Computer System and setting data
$Msvm_VirtualSystemManagementService = Get-WmiObject -Namespace root\virtualization\v2 -Class Msvm_VirtualSystemManagementService
$Msvm_ComputerSystem = Get-WmiObject -Namespace root\virtualization\v2 -Class Msvm_ComputerSystem -Filter "ElementName='$vmName'"
$Msvm_VirtualSystemSettingData = $Msvm_ComputerSystem.GetRelated(

#Get an instance of the port setting data object and the related guest network configuration object
$Msvm_SyntheticEthernetPortSettingData = $Msvm_VirtualSystemSettingData.GetRelated("Msvm_SyntheticEthernetPortSettingData")
$Msvm_GuestNetworkAdapterConfigurations = $Msvm_SyntheticEthernetPortSettingData.GetRelated(
$Msvm_GuestNetworkAdapterConfiguration = ($Msvm_GuestNetworkAdapterConfigurations | % {$_})

#Set the IP address and related information
$Msvm_GuestNetworkAdapterConfiguration.DHCPEnabled = $false
$Msvm_GuestNetworkAdapterConfiguration.IPAddresses = @("")
$Msvm_GuestNetworkAdapterConfiguration.Subnets = @("")
$Msvm_GuestNetworkAdapterConfiguration.DefaultGateways = @("")
$Msvm_GuestNetworkAdapterConfiguration.DNSServers = @("", "")

#Set the IP address