Skip to content

Instantly share code, notes, and snippets.

@andrii-riabchun
Created September 19, 2016 17:34
Show Gist options
  • Save andrii-riabchun/ea28a6d7db7de068c6b60caf8a99ee37 to your computer and use it in GitHub Desktop.
Save andrii-riabchun/ea28a6d7db7de068c6b60caf8a99ee37 to your computer and use it in GitHub Desktop.
Powershell Script for easy toggling Microsoft Hyper-V Windows Feature
#Requires -RunAsAdministrator
# Script for enabling/disabling Hyper-V.
Param(
[string]$action
)
function usage {
Write-Host "usage:
$($args[0]) status - hyper-v status
$($args[0]) enable - enables hyper-v with all tools
$($args[0]) disable - disables hyper-v with all tools"
}
function getFeatures {
return Get-WindowsOptionalFeature -Online `
| Where-Object {$_.FeatureName.StartsWith("Microsoft-Hyper-V")}
}
switch ($action) {
"status" { getFeatures | Format-List }
"enable" {
Enable-WindowsOptionalFeature -Online -FeatureName $(
getFeatures | ForEach-Object {"$($_.FeatureName)"}
)
}
"disable" {
Disable-WindowsOptionalFeature -Online -FeatureName $(
getFeatures | ForEach-Object {"$($_.FeatureName)"}
)
}
Default {usage}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment