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.
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.
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.
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.
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."
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With