-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathReimage.ps1
192 lines (159 loc) · 7.72 KB
/
Reimage.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
function Get-IsElevated {
$id = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$p = New-Object System.Security.Principal.WindowsPrincipal($id)
if ($p.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator))
{ Write-Output $true }
else
{ Write-Output $false }
}
function Reimage {
if (-not(Get-IsElevated)) {
throw "Please run this script as an administrator"
}
# Get a drive, get the WIM file.
$systemDrive = (Get-WmiObject Win32_OperatingSystem).SystemDrive.Trim(':')
# Get disk
Write-Host "Please provide me a clean disk amount point. Example: 'Q': " -ForegroundColor Yellow
$diskMount = $(Read-Host).Trim(':')
# Ensure disk exists
if (Test-Path -Path "$($diskMount):\") {
Write-Host "Disk $diskMount exists!" -ForegroundColor Green
} else {
throw "Disk $diskMount doesn't exist!"
}
if ($systemDrive.ToLower() -eq $diskMount.ToLower()) {
throw "You can't install new OS on your existing OS drive: $diskMount!"
}
# Ensure disk enough size
if ((Get-Volume $diskMount).Size -lt 20000000000) {
throw "Disk $diskMount too mall! Please assign at least 20GB!"
}
# Format to NTFS.
Get-ChildItem "$($diskMount):\" -ErrorAction SilentlyContinue
Write-Host "Enter 'Y' if you want to format disk $diskMount [Y or N]:" -ForegroundColor Yellow
$format = Read-Host
if ($format -eq "Y") {
Format-Volume -DriveLetter $diskMount -FileSystem NTFS
} else {
throw "You must format that disk first!"
}
# Disable Bitlocker
Disable-BitLocker -MountPoint $diskMount
do {
Write-Host "We need the Windows image file. What do you have now?`n" -ForegroundColor Yellow
Write-Host -NoNewline "A: " -ForegroundColor White
Write-Host "I have nothing. Help me download the new OS."
Write-Host -NoNewline "B: " -ForegroundColor White
Write-Host "I have nothing. Tell me how to download the new OS. (I will manually download it)"
Write-Host -NoNewline "C: " -ForegroundColor White
Write-Host "I already have the ISO file downloaded locally."
Write-Host -NoNewline "D: " -ForegroundColor White
Write-Host "I already have the install.wim file locally.`n"
$userOption = Read-Host -Prompt 'Select'
if($userOption.Length -eq 1 -and $userOption.ToLower() -ge "a" -and $userOption.ToLower() -le "d") {
break
} else {
Write-Host "Invalid input!" -ForegroundColor Red
}
} until($false)
if ($userOption.ToLower() -eq "a") {
Write-Host "To download Insider builds, you can go to https://uupdump.net/ ." -ForegroundColor Green
Start-Process powershell {
Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://raw.githubusercontent.com/pbatard/Fido/master/Fido.ps1'))
}
Read-Host "Press [Enter] if you finished downloading the ISO file."
}
if ($userOption.ToLower() -eq "b") {
Write-Host "Please open the following link to download Windows ISO:`n" -ForegroundColor Yellow
Write-Host -NoNewline "Download Windows 10: " -ForegroundColor White
Write-Host "https://www.microsoft.com/en-US/software-download/windows10" -ForegroundColor DarkBlue
Write-Host -NoNewline "Download Windows 11: " -ForegroundColor White
Write-Host "https://www.microsoft.com/en-us/software-download/windows11" -ForegroundColor DarkBlue
Write-Host -NoNewline "Download Windows Insider: " -ForegroundColor White
Write-Host "Download Windows Insider: https://www.microsoft.com/en-us/software-download/windowsinsiderpreviewiso" -ForegroundColor DarkBlue
Read-Host "Press [Enter] if you finished downloading the ISO file."
}
if ($userOption.ToLower() -eq "a" -or $userOption.ToLower() -eq "b" -or $userOption.ToLower() -eq "c") {
# Enlist ISO options
Write-Host "All ISO files here ($($(Get-Location))): " -ForegroundColor White
Get-ChildItem -Filter "*.iso" | Format-Table -AutoSize
Write-Host "`nPlease provide me the path of your ISO file (ends with .iso):" -ForegroundColor Yellow
$iso = Read-Host
$iso = (Resolve-Path $iso).Path
if (Test-Path -Path "$iso") {
Get-Item "$iso" | Format-List
Write-Host "ISO $iso exists!" -ForegroundColor Green
} else {
throw "ISO $iso doesn't exist! Please check your path!"
}
# Mount ISO
$mounted = Mount-DiskImage -ImagePath $iso -Access ReadOnly -StorageType ISO
$mountedISO = Get-Volume -DiskImage $mounted
Write-Host "Mounted:" -ForegroundColor Green
$mountedISO | Format-List
$mountedLetter = $mountedISO.DriveLetter
Write-Host "Files inside:" -ForegroundColor Green
Get-ChildItem "$($mountedLetter):" | Format-Table -AutoSize
# Get OS Index
$wimFile = "$($mountedLetter):\sources\install.wim"
}
if ($userOption.ToLower() -eq "d") {
# Enlist ISO options
Write-Host "All WIM files here ($($(Get-Location))): " -ForegroundColor White
Get-ChildItem -Filter "*.wim" | Format-Table -AutoSize
Write-Host "`nPlease provide me the path of your WIM file:" -ForegroundColor Yellow
$wim = Read-Host
$wim = (Resolve-Path $wim).Path
if (Test-Path -Path "$wim") {
Get-Item "$wim" | Format-List
Write-Host "WIM $wim exists!" -ForegroundColor Green
} else {
throw "WIM $wim doesn't exist!"
}
$wimFile = $wim
}
dism /Get-ImageInfo /imagefile:"$wimFile"
Write-Host "Please provide the OS Index number. Example: '6': " -ForegroundColor Yellow
$osIndex = Read-Host
# Get OS Name
Write-Host "Please name the new OS. Example: Windows VNext: " -ForegroundColor Yellow
$osName = Read-Host
Write-Host "Extracting OS..." -ForegroundColor Green
dism /apply-image /imagefile:"$wimFile" /index:"$osIndex" /ApplyDir:"$($diskMount):\"
# Dismount ISO
if ($iso) {
Write-Host "Dismounting the iso..." -ForegroundColor Green
Dismount-DiskImage $iso -ErrorAction SilentlyContinue
}
# Create start up registry.
$created = bcdedit /create /d "$osName" /application osloader
$osID = $created | Select-String -Pattern '{[-0-9A-F]+?}' -AllMatches | Select-Object -ExpandProperty Matches | Select-Object -ExpandProperty Value
bcdedit /set "$osID" device "partition=$($diskMount):"
bcdedit /set "$osID" path "\WINDOWS\system32\winload.efi"
bcdedit /set "$osID" systemroot "\WINDOWS"
bcdedit /set "$osID" osdevice "partition=$($diskMount):"
bcdedit /set "$osID" locale "en-US"
bcdedit /set "$osID" inherit "{bootloadersettings}"
bcdedit /set "$osID" nx "OptIn"
bcdedit /set "$osID" bootmenupolicy "Standard"
bcdedit /set "$osID" displaymessageoverride "Recovery"
bcdedit /set "$osID" recoveryenabled "Yes"
bcdedit /set "$osID" isolatedcontext "Yes"
bcdedit /set "$osID" flightsigning "Yes"
bcdedit /set "$osID" allowedinmemorysettings "0x15000075"
bcdedit /displayorder "$osID" /addlast
bcdedit /set "{bootmgr}" default "$osID"
bcdedit /set "{current}" bootmenupolicy legacy
bcdedit /set "{default}" bootmenupolicy legacy
Write-Host "Modified boot configuration:" -ForegroundColor Green
bcdedit
# Disable Bitlocker
Disable-BitLocker -MountPoint $diskMount
Write-Host "Unmounting hard disk..." -ForegroundColor Green
mountvol "$($diskMount):" /P
Write-Host "Job finished! Pending reboot!" -ForegroundColor Green
Write-Host "Press Enter to reboot now..." -ForegroundColor Yellow
Read-Host
Restart-Computer -Force
}
Reimage