Introduction
Deleting the recovery partition can be necessary for various reasons, such as reclaiming disk space or reconfiguring disk layout. PowerShell provides a powerful and efficient way to manage disk partitions programmatically. Below is a step-by-step guide and a PowerShell script to automate this process.
PowerShell Script for Deleting the Recovery Partition
The following PowerShell script detects the recovery partition, deletes it, and resizes the primary partition (typically the C: drive) to utilize the freed space.
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 | # Get all partitions $Partitions = Get-Partition # Detect the C: drive partition number $PartLettreC = 99 Foreach($Partition in $Partitions) { $Letterpart = $Partition.DriveLetter $Numberpart = $Partition.PartitionNumber If ($Letterpart -eq "C") { $PartLettreC = $Numberpart } } # Initialize a flag to check if a recovery partition was found and deleted $Okrecovery = 0 Foreach($Partition in $Partitions) { $typepart = $Partition.Type $Numberpart = $Partition.PartitionNumber if ($typepart -eq "Recovery") { Remove-Partition -DiskNumber 0 -PartitionNumber $Numberpart -confirm:$False $Okrecovery = 1 } } # Resize the C: partition if the recovery partition was deleted if ($Okrecovery -eq 1) { $size = (Get-PartitionSupportedSize -DiskNumber 0 -PartitionNumber $PartLettreC) Resize-Partition -DiskNumber 0 -PartitionNumber $PartLettreC -Size $size.SizeMax } |
Script Explanation
- Get-Partition: Retrieves all partitions on the system.
- Detect the C: drive partition number: Identifies the partition number associated with the C: drive.
- Find and remove the recovery partition: Iterates through the partitions, detects the recovery partition, and removes it.
- Resize the C: partition: If the recovery partition is deleted, resizes the C: partition to utilize the available space.
0 Comments