You can use the Get-View cmdlet to update a view object by using the information from a previously called managed object.

Connect to a vSphere server.

1

Get the VM2 virtual machine by name.

$vm2 = Get-View -ViewType VirtualMachine -Filter @{"Name" = "VM2}
$hostView = Get-View -Id $vm2.Runtime.Host
2

View the current power state.

$vm2.Runtime.PowerState
3

Power off the virtual machine.

If ($vm2.Runtime.PowerState -ne “PoweredOn”) {
$vm.PowerOnVM($vm2.Runtime.Host)
} else {
$vm2.PowerOffVM()
}
4

View the value of $vm2 power state.

$vm2.Runtime.PowerState

The power state is still not updated because the virtual machine property values are not updated automatically.

5

Update the view object.

$vm2.UpdateViewData()
6

Obtain the actual power state of the virtual machine.

$vm2.Runtime.PowerState