| 147 | If the computer is used for other things that may require Hyper-V a PowerShell script may be used to switch Hyper-V on / off |
| 148 | {{{ |
| 149 | SetHyper-V.ps1 |
| 150 | |
| 151 | param([string]$state='Off') |
| 152 | 'Set Hyper-V ' + $state |
| 153 | $lines = bcdedit |
| 154 | ForEach($line in $lines) { |
| 155 | $pos = $line.IndexOf(' ') |
| 156 | If($pos -gt 0) { |
| 157 | $prompt = $line.Substring(0, $pos) |
| 158 | $curstate = $line.Substring($pos).Trim() |
| 159 | If($prompt -eq 'hypervisorlaunchtype') { |
| 160 | '[' + $prompt + '] = [' + $curstate + ']' |
| 161 | If($curstate -ne $state) { |
| 162 | 'Setting hypervisorlauchtype to ' + $state |
| 163 | $result = bcdedit /set hypervisorlaunchtype $state |
| 164 | 'Result = [' + $result + ']' |
| 165 | If($result -eq 'The operation completed successfully.') { |
| 166 | 'Restarting in two seconds' |
| 167 | Start-Sleep -s 2 |
| 168 | Restart-Computer |
| 169 | } Else { |
| 170 | 'Error setting state' |
| 171 | Start-Sleep -s 5 |
| 172 | } |
| 173 | } Else { |
| 174 | 'Hypervisor launch type is already ' + $state |
| 175 | Start-Sleep -s 5 |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | }}} |
| 181 | |
| 182 | Create a folder C:\scripts if it does not already exist and add this Powershell script to the folder. |
| 183 | |
| 184 | Create a shortcut on the desktop named "Hyper-V On" and set it the Target to: |
| 185 | C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe C:\Scripts\SetHyper-V.ps1 -state Auto |
| 186 | |
| 187 | Create a shortcut on the desktop named "Hyper-V Off" and set it the Target to: |
| 188 | C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe C:\Scripts\SetHyper-V.ps1 -state Off |