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.