How do you check to see if Hyper-V is enabled using PowerShell?
35,427
Solution 1
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
Solution 2
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."
}
Solution 3
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) -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
}
}
Author by
Evan Amara
Updated on October 25, 2020Comments
-
Evan Amara over 3 years
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 almost 8 yearsThis fixed it, thanks. It seems like the = was resetting the value of $hyperv.
-
c-chavez over 6 years@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 ss64.com/ps/syntax-compare.html
-
Jonathan Gagne over 5 yearsUsing PowerShell
-
Lee_Dailey over 5 yearsthe OP asked for help with doing this programmatically, preferably using powershell. your method is not at all suitable for the scenario listed - and that scenario is fairly sensible. [grin]
-
Pablo Jomer over 4 yearsIs 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.