Files

142 lines
4.7 KiB
PowerShell

Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"
$BackupDir = "H:\Windows-Neuaufsetzen-Backup\12_Exportierte_Listen"
$BeforeFile = Join-Path $BackupDir "bcdedit_enum_before_dualboot_cleanup.txt"
$AfterFile = Join-Path $BackupDir "bcdedit_enum_after_dualboot_cleanup.txt"
function Assert-Admin {
$Identity = [Security.Principal.WindowsIdentity]::GetCurrent()
$Principal = [Security.Principal.WindowsPrincipal]::new($Identity)
if (-not $Principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
throw "Dieses Skript muss in PowerShell als Administrator ausgefuehrt werden."
}
}
function Get-BcdOsLoaderEntries {
$Text = & bcdedit /enum osloader /v
if ($LASTEXITCODE -ne 0) {
throw "bcdedit /enum osloader /v ist fehlgeschlagen."
}
$Entries = @()
$Current = $null
foreach ($Line in $Text) {
if ($Line -match "^-{5,}$") {
if ($Current -and $Current.Identifier) {
$Entries += [pscustomobject]$Current
}
$Current = @{
Identifier = $null
Device = $null
OsDevice = $null
Path = $null
Description = $null
}
continue
}
if (-not $Current) {
continue
}
if ($Line -match "^\s*(identifier|Bezeichner)\s+(.+)$") { $Current.Identifier = $Matches[2].Trim(); continue }
if ($Line -match "^\s*device\s+(.+)$") { $Current.Device = $Matches[1].Trim(); continue }
if ($Line -match "^\s*osdevice\s+(.+)$") { $Current.OsDevice = $Matches[1].Trim(); continue }
if ($Line -match "^\s*path\s+(.+)$") { $Current.Path = $Matches[1].Trim(); continue }
if ($Line -match "^\s*description\s+(.+)$") { $Current.Description = $Matches[1].Trim(); continue }
}
if ($Current -and $Current.Identifier) {
$Entries += [pscustomobject]$Current
}
return $Entries
}
Assert-Admin
New-Item -ItemType Directory -Force -Path $BackupDir | Out-Null
& bcdedit /enum all /v | Out-File $BeforeFile -Encoding UTF8
$Entries = Get-BcdOsLoaderEntries
Write-Host "Gefundene Windows-Boot-Eintraege:"
$Entries | Select-Object Identifier, Description, Device, OsDevice, Path | Format-Table -AutoSize
$NewWindows = $Entries | Where-Object {
$_.OsDevice -eq "partition=D:" -and
$_.Path -match "winload\.efi$" -and
(Test-Path "D:\Windows\System32\winload.efi")
} | Select-Object -First 1
$OldWindows = $Entries | Where-Object {
$_.OsDevice -eq "partition=C:" -and
$_.Path -match "winload\.efi$" -and
(Test-Path "C:\Windows\System32\winload.efi")
} | Select-Object -First 1
if (-not $NewWindows) {
throw "Abbruch: Neuer Windows-Eintrag auf D:\Windows wurde nicht eindeutig gefunden."
}
if (-not $OldWindows) {
throw "Abbruch: Alter Windows-Eintrag auf C:\Windows wurde nicht eindeutig gefunden."
}
$KeepIds = @($NewWindows.Identifier, $OldWindows.Identifier)
$DeleteEntries = $Entries | Where-Object { $KeepIds -notcontains $_.Identifier }
Write-Host ""
Write-Host "Bleibt erhalten:"
Write-Host " NEU: $($NewWindows.Identifier) -> $($NewWindows.OsDevice) -> D:\Windows"
Write-Host " ALT: $($OldWindows.Identifier) -> $($OldWindows.OsDevice) -> C:\Windows"
Write-Host ""
Write-Host "Wird aus dem Bootmenue entfernt:"
if ($DeleteEntries) {
$DeleteEntries | Select-Object Identifier, Description, Device, OsDevice, Path | Format-Table -AutoSize
} else {
Write-Host " Keine ueberfluessigen Eintraege gefunden."
}
Write-Host ""
Write-Host "Es werden nur Bootmenue-Eintraege geloescht, keine Partitionen und keine Windows-Ordner."
$Confirmation = Read-Host "Tippe exakt BOOTCLEAN um fortzufahren"
if ($Confirmation -ne "BOOTCLEAN") {
throw "Abbruch: Bestaetigung wurde nicht eingegeben."
}
& bcdedit /set $NewWindows.Identifier description "Windows 11 Neu"
if ($LASTEXITCODE -ne 0) { throw "Konnte neuen Windows-Eintrag nicht umbenennen." }
& bcdedit /set $OldWindows.Identifier description "Windows 11 Alt"
if ($LASTEXITCODE -ne 0) { throw "Konnte alten Windows-Eintrag nicht umbenennen." }
& bcdedit /default $NewWindows.Identifier
if ($LASTEXITCODE -ne 0) { throw "Konnte Standard-Boot-Eintrag nicht setzen." }
& bcdedit /timeout 5
if ($LASTEXITCODE -ne 0) { throw "Konnte Timeout nicht setzen." }
foreach ($Entry in $DeleteEntries) {
Write-Host "Loesche Boot-Eintrag $($Entry.Identifier) ($($Entry.Description))"
& bcdedit /delete $Entry.Identifier /f
if ($LASTEXITCODE -ne 0) {
throw "Konnte Boot-Eintrag $($Entry.Identifier) nicht loeschen."
}
}
& bcdedit /enum all /v | Out-File $AfterFile -Encoding UTF8
Write-Host ""
Write-Host "Bootmenue bereinigt."
Write-Host "Vorher gesichert: $BeforeFile"
Write-Host "Nachher gesichert: $AfterFile"
Write-Host ""
Write-Host "Bitte neu starten. Es sollten nur noch zwei Eintraege erscheinen:"
Write-Host " Windows 11 Neu"
Write-Host " Windows 11 Alt"