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

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
    }
}

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."
}

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