Write-Output "Shutting down all Virtual Machines..." If (((Get-CimInstance Win32_OperatingSystem).Caption -match 'Microsoft Windows 10') -or ((Get-CimInstance Win32_OperatingSystem).Caption -match 'Microsoft Windows 8')) { If ((Get-WindowsOptionalFeature -FeatureName Microsoft-Hyper-V-All -Online).State -eq 'Enabled') { Write-Output "Client Hyper-V is enabled." $HVHost = "Yes" } ElseIf ((Get-WindowsOptionalFeature -FeatureName Microsoft-Hyper-V-All -Online).State -eq 'Disabled') { Write-Output "Client Hyper-V is not enabled." } Else { Throw "Unable to determine Client Hyper-V state, aborting." } } ElseIf ((Get-CimInstance Win32_OperatingSystem).Caption -match 'Microsoft Windows Server') { If ((Get-WindowsFeature -Name Hyper-V) -eq $true) { Write-Output "Server Hyper-V is enabled." $HVHost = "Yes" } ElseIf ((Get-WindowsFeature -Name Hyper-V) -eq $false) { Write-Output "Server Hyper-V is not enabled." } Else { Throw "Unable to determine Server Hyper-V state, aborting." } } Else { Throw "Unable to determine Operating system to check Hyper-V state, aborting." } Try { If ($HVHost -eq "Yes") { Write-Output "Shutting down all Virtual Machines..." Do { $RunningVMs= Get-VM | Where-Object -Property State -eq "Running" Foreach ($RunningVM in $RunningVMs) { Write-Host (Get-Date)": Shutdown initiated on $($RunningVM.Name)..." Stop-VM $RunningVM -AsJob | Out-Null } If ($RunningVMs) { Write-Host (Get-Date)": Virtual Machine shutdown in progress, re-check in 1 minute..." Start-Sleep -Seconds 60 } } Until ($null -eq $RunningVMs) Write-Host (Get-Date)": All Virtual Machines have been shut down." Write-Host (Get-Date)": Shutting down host..." #Restart-Computer -Force } } Catch { Throw "" }