-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRefusePS.ps1
More file actions
162 lines (122 loc) · 4.67 KB
/
RefusePS.ps1
File metadata and controls
162 lines (122 loc) · 4.67 KB
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
#requires -RunAsAdministrator
<#
.SYNOPSIS
RefusePS - Create a bootable Windows USB installer from a Windows ISO.
.AUTHOR
h4rithd
.DESCRIPTION
RefusePS prepares a USB drive for Windows installation by:
- Selecting a target USB disk safely
- Wiping and reinitializing the selected USB disk
- Formatting it as FAT32 for UEFI boot
- Mounting a Windows ISO
- Copying ISO contents to USB
- Splitting install.wim into .swm files when needed
.WARNING
This script will erase the selected USB disk.
Double-check the disk number before confirming.
#>
$isoImage = "C:\Path\To\windows10.iso"
$ErrorActionPreference = "Stop"
Write-Host ""
Write-Host "RefusePS by h4rithd" -ForegroundColor Cyan
Write-Host "Windows Bootable USB Creator" -ForegroundColor Cyan
Write-Host ""
if (-not (Test-Path $isoImage)) {
throw "ISO file not found: $isoImage"
}
$usbDisks = Get-Disk | Where-Object BusType -eq "USB"
if (-not $usbDisks) {
throw "No USB disks found."
}
Write-Host "Detected USB disks:" -ForegroundColor Yellow
$usbDisks | Format-Table Number, FriendlyName, SerialNumber, Size, PartitionStyle -AutoSize
$diskNumber = Read-Host "Enter the USB disk number to wipe"
if ($diskNumber -notmatch '^\d+$') {
throw "Invalid disk number."
}
$targetDisk = Get-Disk -Number $diskNumber -ErrorAction Stop
if ($targetDisk.BusType -ne "USB") {
throw "Disk $diskNumber is not a USB disk. Aborting."
}
Write-Warning "This will ERASE all data on USB disk $diskNumber: $($targetDisk.FriendlyName)"
$confirm = Read-Host "Type ERASE to continue"
if ($confirm -ne "ERASE") {
throw "Operation cancelled."
}
$mountedIso = $null
$usbVolume = $null
try {
Write-Host "Preparing USB disk..." -ForegroundColor Yellow
$formattedVolume = $targetDisk |
Clear-Disk -RemoveData -RemoveOEM -Confirm:$false -PassThru |
Initialize-Disk -PartitionStyle GPT -PassThru |
New-Partition -UseMaximumSize -AssignDriveLetter |
Format-Volume -FileSystem FAT32 -NewFileSystemLabel "REFUSEPS" -Confirm:$false
$usbVolume = $formattedVolume
$usbRoot = "$($usbVolume.DriveLetter):\"
if (-not (Test-Path $usbRoot)) {
throw "USB volume was not mounted correctly."
}
Write-Host "Mounting ISO..." -ForegroundColor Yellow
$mountedIso = Mount-DiskImage -ImagePath $isoImage -StorageType ISO -PassThru
$isoVolume = $mountedIso | Get-Volume
$isoRoot = "$($isoVolume.DriveLetter):\"
if (-not (Test-Path $isoRoot)) {
throw "ISO volume was not mounted correctly."
}
$installWim = Join-Path $isoRoot "sources\install.wim"
$installEsd = Join-Path $isoRoot "sources\install.esd"
$usbSources = Join-Path $usbRoot "sources"
Write-Host "Copying ISO files to USB..." -ForegroundColor Yellow
robocopy $isoRoot $usbRoot /S /R:0 /W:0 /Z /XF install.wim install.esd /NP
$robocopyExit = $LASTEXITCODE
if ($robocopyExit -ge 8) {
throw "Robocopy failed with exit code $robocopyExit"
}
if (-not (Test-Path $usbSources)) {
New-Item -Path $usbSources -ItemType Directory | Out-Null
}
if (Test-Path $installWim) {
Write-Host "Splitting install.wim for FAT32 compatibility..." -ForegroundColor Yellow
dism /Split-Image `
/ImageFile:$installWim `
/SWMFile:"$usbSources\install.swm" `
/FileSize:3800
if ($LASTEXITCODE -ne 0) {
throw "DISM failed while splitting install.wim"
}
}
elseif (Test-Path $installEsd) {
Write-Host "Copying install.esd..." -ForegroundColor Yellow
$esdSize = (Get-Item $installEsd).Length
if ($esdSize -gt 4GB) {
throw "install.esd is larger than 4GB. FAT32 cannot store files larger than 4GB."
}
Copy-Item $installEsd -Destination "$usbSources\install.esd" -Force
}
else {
throw "Neither install.wim nor install.esd found in ISO sources directory."
}
Write-Host ""
Write-Host "RefusePS completed successfully." -ForegroundColor Green
Write-Host "Bootable Windows USB created at $usbRoot" -ForegroundColor Green
}
finally {
if ($mountedIso) {
Write-Host "Dismounting ISO..." -ForegroundColor Yellow
Dismount-DiskImage -ImagePath $isoImage -ErrorAction SilentlyContinue
}
if ($usbVolume -and $usbVolume.DriveLetter) {
try {
Write-Host "Ejecting USB..." -ForegroundColor Yellow
(New-Object -ComObject Shell.Application).
NameSpace(17).
ParseName("$($usbVolume.DriveLetter):").
InvokeVerb("Eject")
}
catch {
Write-Warning "Could not eject USB automatically. Eject it manually."
}
}
}