-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Install-HyperV.ps1
110 lines (87 loc) · 2.39 KB
/
Install-HyperV.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<#
.SYNOPSIS
Automates the installation of Hyper-V. Works on either Pro or Home editions
.DESCRIPTION
This will force an automatic reboot and will pick up where it left off to
complete the configuration.
.NOTES
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
#>
# CmdletBinding adds -Verbose functionality, SupportsShouldProcess adds -WhatIf
[CmdletBinding(SupportsShouldProcess = $true)]
param (
[switch] $Continuation
)
Begin
{
. $PSScriptRoot\common.ps1
function CheckPrerequisites
{
if (!(IsElevated))
{
Write-Host
WriteWarn '... This script must be run from an elevated console'
WriteWarn '... Open an administrative PowerShell window and run again'
return $false
}
if (!(IsWindows11))
{
Write-Host
WriteWarn '... This script only applies to Windows 11'
return $false
}
# Already installed?
if ((Get-WindowsOptionalFeature -FeatureName Microsoft-Hyper-V-All -Online).State -eq 'Enabled')
{
Write-Host
Write-Host '... Hyper-V is ready for use' -ForegroundColor Green
return $false
}
return $true
}
function AddFeaturePackages
{
Highlight '... Patching Home edition with Hyper-V feature packages'
(Get-ChildItem $env:SystemRoot\servicing\packages\*Hyper-V*.mum).Name | `
foreach {
dism /online /norestart /add-package:"$($env:SystemRoot)\servicing\packages\$_"
}
}
function EnableHyperV
{
Highlight '... Enabling Hyper-V; will prompt to reboot to complete configuration'
if (IsWindowsProEdition)
{
Enable-WindowsOptionalFeature -Online -FeatureName containers -All -NoRestart
}
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V -All -NoRestart
}
function CustomizeConfiguration
{
# set custom Hyper-V host file locations
Set-VMHost -VirtualMachinePath 'C:\VMs' -VirtualHardDiskPath 'C:\VMs\Disks'
Write-Host
WriteOK '... Hyper-V configuration is complete'
Read-Host '... Press Enter to continue'
}
}
Process
{
if ($Continuation)
{
CleanupContinuation
CustomizeConfiguration
return
}
if (!(CheckPrerequisites))
{
return
}
if (IsWindowsHomeEdition)
{
AddFeaturePackages
}
EnableHyperV
RebootWithContinuation
}