Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you check to see if Hyper-V is enabled using PowerShell?

I am trying to write a PowerShell script that checks the Windows Optional Features to see if Hyper-V is installed. However, my code is not working. Even when Hyper-V is disabled, the script outputs that it is already enabled.

#Requires -RunAsAdministrator

# Get the Hyper-V feature and store it in $hyperv
$hyperv = Get-WindowsOptionalFeature -FeatureName Microsoft-Hyper-V-All -Online

# Check if Hyper-V is already enabled.
if($hyperv.State = "Enabled") {
    Write-Host "Hyper-V is already enabled."
} else {
    Write-Host "Hyper-V is disabled."
}

There is no error when the code is run.

like image 463
Evan Amara Avatar asked Jun 01 '16 11:06

Evan Amara


People also ask

How do you check Hyper-V is enabled or not?

Right click on the Windows button and select 'Apps and Features'. Select Programs and Features on the right under related settings. Select Turn Windows Features on or off. Select Hyper-V and click OK.

What PowerShell command can you use to check if a Hyper-V is installed?

To see all VMs on the local Hyper-V host, you should run the Get-VM cmdlet. On the PowerShell screen, you can see the list of available VMs, including their name, state, CPU usage, memory assigned, uptime, status, and version.

How do I know if my Hyper-V server is running?

Click Start, click Administrative Tools, and then click Event Viewer. Open the Hyper-V-Hypervisor event log. In the navigation pane, expand Applications and Services Logs, expand Microsoft, expand Hyper-V-Hypervisor, and then click Operational. If Windows hypervisor is running, no further action is needed.


1 Answers

Here's the full powershell script that works for me. Just copy and paste it into an elevated powershell then press enter.

$hyperv = Get-WindowsOptionalFeature -FeatureName Microsoft-Hyper-V-All -Online
# Check if Hyper-V is enabled
if($hyperv.State -eq "Enabled") {
    Write-Host "Hyper-V is enabled."
} else {
    Write-Host "Hyper-V is disabled."
}
like image 167
Jon Avatar answered Oct 17 '22 05:10

Jon