16

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.

Evan Amara
  • 163
  • 1
  • 1
  • 6

4 Answers4

23

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."
}
Jon
  • 9,156
  • 9
  • 56
  • 73
11

I believe it has to do with your if condition, try this:

if($hyperv.State -eq "Enabled")

The = sign is not going to work, you need to do it PowerShell way

Andrey Marchuk
  • 13,301
  • 2
  • 36
  • 52
  • This fixed it, thanks. It seems like the = was resetting the value of $hyperv. – Evan Amara Jun 01 '16 at 11:52
  • @EvanAmara In most programming languages one equal sign (=) assigns a value and 2 equal signs (==) compare values. In powershell -eq (meaning equal) compares if 2 values are equal. If you need to know if the values are different you can use -ne (not equal). Check this website for more info on comparing values https://ss64.com/ps/syntax-compare.html – c-chavez Sep 23 '17 at 10:23
  • Is it possible to check if hyper-v is also running? Usually this requires a reboot and until then this command reports that the feature is enabled even if it's not actually running. – Pablo Jomer Sep 04 '19 at 06:26
4

For Windows 10 Pro / Education / Enterprise

if ((Get-WindowsOptionalFeature -FeatureName Microsoft-Hyper-V-All -Online).State -ne 'Enabled')
{
    Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V -All
}

For Windows Server

if ((Get-WindowsFeature -Name Hyper-V).Installed -eq $false)
{
    Install-WindowsFeature -Name Hyper-V -IncludeManagementTools
}

Generic script

Write-Host "Enabling Hyper-V in host..."
if ((Get-CimInstance Win32_OperatingSystem).Caption -match 'Microsoft Windows 10')
{
    if ((Get-WindowsOptionalFeature -FeatureName Microsoft-Hyper-V-All -Online).State -ne 'Enabled')
    {
        Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V -All
    }
}
if ((Get-CimInstance Win32_OperatingSystem).Caption -match 'Microsoft Windows Server')
{

    if ((Get-WindowsFeature -Name Hyper-V) -eq $false)
    {
        Install-WindowsFeature -Name Hyper-V -IncludeManagementTools
    }
}
Grant
  • 127
  • 9
KUTlime
  • 5,889
  • 1
  • 18
  • 29
-7

An easier way to do that is to go to Services by clicking the Start button and typing Services.msc and scrolling down to the Hyper-V Host Compute Service and seeing if it running. Also check the Hyper-V Virtual Machine Management service.

If they are both running you can safely assume that Hyper-V is running and active. My machine Windows 10 Pro with VMWARE Workstation 14.