Skip to content

Instantly share code, notes, and snippets.

@heyjoeway
Last active January 19, 2024 18:32
Show Gist options
  • Save heyjoeway/bccacb8988305c0fbe3a652998d3300b to your computer and use it in GitHub Desktop.
Save heyjoeway/bccacb8988305c0fbe3a652998d3300b to your computer and use it in GitHub Desktop.
# Automates KB5028997
# https://support.microsoft.com/en-us/topic/kb5028997-instructions-to-manually-resize-your-partition-to-install-the-winre-update-400faa27-9343-461c-ada9-24c8229763bf
# WARNING: THIS SCRIPT WILL CHANGE YOUR PARTITION LAYOUT. USE AT YOUR OWN RISK.
# NOT RESPONSIBLE FOR ANY DATA LOSS.
function KB5028997 {
$TargetSize = 750MB
$ReagentC = reagentc /info
# Get line containing "Windows RE location"
$WinRELocation = $ReagentC | Select-String -Pattern "Windows RE location"
# Get the location
$WinRELocation = $WinRELocation -split ":"
$WinRELocation = $WinRELocation[1].Trim()
# Split the string by "\"
$splitLocation = $WinRELocation -split "\\"
# Find the element that starts with "harddisk"
$harddiskElement = $splitLocation | Where-Object { $_ -like "harddisk*" }
# Extract the number after "harddisk"
$WinREDiskNumber = $harddiskElement | Select-String -Pattern "\d+" -AllMatches | ForEach-Object { $_.Matches } | ForEach-Object { $_.Value }
# Find the element that starts with "partition"
$partitionElement = $splitLocation | Where-Object { $_ -like "partition*" }
# Extract the number after "partition"
$WinREPartitionNumber = $partitionElement | Select-String -Pattern "\d+" -AllMatches | ForEach-Object { $_.Matches } | ForEach-Object { $_.Value }
# Get size of WinRE partition
$WinREPartitionSize = (Get-PartitionSupportedSize -DiskNumber $WinREDiskNumber -PartitionNumber $WinREPartitionNumber).SizeMax
Write-Host "Current WinRE partition size: $WinREPartitionSize"
Write-Host "Target WinRE partition size: $TargetSize"
if ($WinREPartitionSize -ge $TargetSize) {
Write-Host "WinRE partition size is already good!"
return
}
# Get drive letter of disk $WinREDiskNumber and partition $WinREPartitionNumber-1
$DriveLetterBeforeWinRE = (Get-Partition -DiskNumber $WinREDiskNumber -PartitionNumber ($WinREPartitionNumber-1)).DriveLetter
if ($DriveLetterBeforeWinRE -ne "C") {
Write-Host "Partition layout is not compatible with this script. Sorry dude."
return
} else {
Write-Host "Partition layout is compatible with this script. Continuing..."
}
# Get line containing "Windows RE status"
$WinREStatus = $ReagentC | Select-String -Pattern "Windows RE status"
# Get the status
$WinREStatus = $WinREStatus -split ":"
$WinREStatus = $WinREStatus[1].Trim()
if ($WinREStatus -eq "Enabled") {
Write-Host "Windows RE is enabled. Disabling..."
reagentc /disable
}
$ShrinkSize = $TargetSize - $WinREPartitionSize
# Get current system partition size
$SystemPartitionSizeOld = Get-Partition -DriveLetter "C" | Select-Object -ExpandProperty Size
$SystemPartitionSizeNew = $SystemPartitionSizeOld - $ShrinkSize
# Shrink system partition
Write-Host "Shrinking system partition by $ShrinkSize bytes (to $SystemPartitionSizeNew bytes)..."
try {
Resize-Partition -DriveLetter "C" -Size $SystemPartitionSizeNew
} catch {
Write-Host "Failed to shrink system partition. No changes were made."
return
}
# Delete WinRE partition
Write-Host "Deleting WinRE partition..."
try {
Remove-Partition -DiskNumber $WinREDiskNumber -PartitionNumber $WinREPartitionNumber -Confirm:$false
} catch {
Write-Host "Failed to delete WinRE partition. Reverting changes..."
Resize-Partition -DriveLetter "C" -Size $SystemPartitionSizeOld
return
}
# Create new WinRE partition
Write-Host "Creating new WinRE partition..."
try {
$WinREPartition = New-Partition -DiskNumber $WinREDiskNumber -UseMaximumSize -GptType "{de94bba4-06d1-4d40-a16a-bfd50179d6ac}"
} catch {
Write-Host "Failed to create new WinRE partition. NOT ABLE TO AUTO-REVERT CHANGES."
return
}
Write-Host "Setting GPT attributes..."
$null = @"
select disk $($WinREDiskNumber)
select partition $($WinREPartition.PartitionNumber)
gpt attributes=0x8000000000000001
exit
"@ | diskpart.exe
Write-Host "Formatting WinRE partition..."
$WinREPartition | Format-Volume -FileSystem NTFS -NewFileSystemLabel "Windows RE tools" -Confirm:$false
# Enable WinRE
Write-Host "Enabling WinRE..."
reagentc /enable
Write-Host "Done!"
}
KB5028997
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment