Skip to content

Instantly share code, notes, and snippets.

@spddl
Last active June 30, 2023 07:20
Show Gist options
  • Save spddl/f49e0a8f66bc3014a1ebf0238cb8445e to your computer and use it in GitHub Desktop.
Save spddl/f49e0a8f66bc3014a1ebf0238cb8445e to your computer and use it in GitHub Desktop.
Set-StrictMode -Version 3.0
# rewrite of: https://github.com/iidanL/InstallWindowsWithoutUSB
If (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]'Administrator')) {
# Ist kein Admin, wird neu als Admin gestartet
$filename = (Get-ChildItem $PSCommandPath).Name
$CWD = [Environment]::CurrentDirectory
Start-Process powershell.exe -ArgumentList "-NoProfile -NoExit -Command &{cd '$CWD';.\\$filename}" -Verb RunAs
Exit
}
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
[System.Windows.Forms.Application]::EnableVisualStyles()
# https://learn.microsoft.com/de-de/dotnet/api/system.windows.forms.form
$form = New-Object System.Windows.Forms.Form
$form.ShowIcon = $false
$form.MaximizeBox = $false
$form.MinimizeBox = $false
$form.Text = "InstallWindowsWithoutUSB"
$form.Size = New-Object System.Drawing.Size(377, 205)
$form.StartPosition = 'CenterScreen' # https://learn.microsoft.com/de-de/dotnet/api/system.windows.forms.form.startposition
$form.FormBorderStyle = 'FixedDialog' # https://learn.microsoft.com/de-de/dotnet/api/system.windows.forms.formborderstyle
# https://learn.microsoft.com/de-de/dotnet/api/system.windows.forms.label
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(11, 20)
$label.Size = New-Object System.Drawing.Size(255, 15)
$label.Text = "Select Drive Letter for Windows which you created before`n(You can't select your partition with already installed Windows)."
$label.AutoSize = $true
$form.Controls.Add($label)
$ComboBoxPartition = New-Object system.Windows.Forms.ComboBox
$ComboBoxPartition.Top = ($label.Height + 41)
$ComboBoxPartition.Left = 11
$ComboBoxPartition.Width = 220
$ComboBoxPartition.Autosize = $true
[System.IO.DriveInfo]::GetDrives() | Sort-Object Name | ForEach-Object {
if ($_.VolumeLabel -eq "") {
[void] $ComboBoxPartition.Items.Add("$($_.Name): $([math]::round($_.TotalSize /1Gb, 0))GB")
} else {
[void] $ComboBoxPartition.Items.Add("$($_.Name): $([math]::round($_.TotalSize /1Gb, 0))GB $($_.VolumeLabel)")
}
}
$ComboBoxPartition.SelectedIndex = 0
$Form.Controls.Add($ComboBoxPartition)
$checkboxFormat = new-object System.Windows.Forms.checkbox
$checkboxFormat.Location = new-object System.Drawing.Size(11, 100)
$checkboxFormat.Text = "Format Partition?"
$checkboxFormat.Width = 210
$checkboxFormat.Checked = $true
$Form.Controls.Add($checkboxFormat)
$Button = New-Object System.Windows.Forms.Button
$Button.Location = new-object System.Drawing.Size(11, 130)
$Button.Text = "Start"
$Button.Add_Click({
$TargetPartition = $ComboBoxPartition.Items[$ComboBoxPartition.SelectedIndex].SubString(0, 1)
$form.Close()
$form.Dispose()
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.initialDirectory = $PSScriptRoot
$OpenFileDialog.filter = "KirbyOS Image (*.iso)|*iso"
$OpenFileDialog.ShowDialog() | Out-Null
$path = $($OpenFileDialog.FileName)
if ($checkboxFormat.Checked) {
Write-Host "Format-Volume => $TargetPartition ..."
Write-Host ("Format-Volume => $TargetPartition ...: {0:hh}h:{0:mm}m:{0:ss}s" -f (Measure-Command {
Format-Volume -DriveLetter $TargetPartition -FileSystem NTFS -NewFileSystemLabel "KirbyOS" -Force -Confirm:$false
})) -ForegroundColor Yellow
}
if ((Get-Item 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer' -EA Ignore).Property -contains 'NoDriveTypeAutoRun') {
$script:previousValue = (Get-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer' -Name NoDriveTypeAutoRun).NoDriveTypeAutoRun
}
else {
$script:previousValue = -1
}
# Disables Autoplay on CD-ROM drives only
# "NoDriveTypeAutoRun" = dword:0x00000020
Set-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer' -Name NoDriveTypeAutoRun -Value 0x20 -Type DWord
Write-Host "Mount-DiskImage ... $path"
Write-Host ("Mount-DiskImage ...: {0:hh}h:{0:mm}m:{0:ss}s" -f (Measure-Command {
do {
$oldDrive = Get-DiskImage -ImagePath $path
if ($oldDrive.Attached) {
Dismount-DiskImage -ImagePath $path
}
} while ($oldDrive.Attached)
try {
$Global:Disk = Mount-DiskImage -ImagePath $path -PassThru -Access ReadOnly -ErrorAction Stop | Get-Diskimage | Get-Volume
}
catch {
Write-Host "An error occurred:"
Write-Host $_
exit 1
}
})) -ForegroundColor Yellow
if ($script:previousValue -eq -1) {
Remove-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer' -Name NoDriveTypeAutoRun
} else {
Set-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer' -Name NoDriveTypeAutoRun -Value $script:previousValue -Type DWord
}
$DistributionPoint = $($Global:Disk.DriveLetter) + ":"
Write-Host "Expand-WindowsImage => $DistributionPoint ..."
Write-Host ("Expand-WindowsImage => $DistributionPoint ...: {0:hh}h:{0:mm}m:{0:ss}s" -f (Measure-Command {
if (Test-Path -Path $DistributionPoint\sources\install.wim -PathType Leaf) {
Expand-WindowsImage -ImagePath $DistributionPoint\sources\install.wim -ApplyPath "$($TargetPartition):\" -Index 1
}
elseif (Test-Path -Path $DistributionPoint\sources\install.esd -PathType Leaf) {
Expand-WindowsImage -ImagePath $DistributionPoint\sources\install.esd -ApplyPath "$($TargetPartition):\" -Index 1
}
Copy-Item -Path $DistributionPoint\sources\$OEM$\$$\Setup\Scripts\*.* -Destination "$($TargetPartition):\Windows\Setup\Scripts\" -ErrorAction SilentlyContinue
Copy-Item -Path $DistributionPoint\autounattend.xml -Destination "$($TargetPartition):\Windows\System32\Sysprep\unattend.xml" -ErrorAction SilentlyContinue
})) -ForegroundColor Yellow
Write-Host "Dismount-DiskImage ... $path"
Write-Host ("Dismount-DiskImage ...: {0:hh}h:{0:mm}m:{0:ss}s" -f (Measure-Command {
Dismount-DiskImage -ImagePath $path
})) -ForegroundColor Yellow
Start-Process -FilePath bcdboot.exe -ArgumentList "$($TargetPartition):\Windows" -NoNewWindow
Write-Host "Done. Reboot your pc to access your new Windows." -ForegroundColor Green
Pause
}.GetNewClosure())
$Form.Controls.Add($Button)
$form.Topmost = $True
[void] $form.ShowDialog()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment