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.

# 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

  1. Get-Partition: Retrieves all partitions on the system.
  2. Detect the C: drive partition number: Identifies the partition number associated with the C: drive.
  3. Find and remove the recovery partition: Iterates through the partitions, detects the recovery partition, and removes it.
  4. Resize the C: partition: If the recovery partition is deleted, resizes the C: partition to utilize the available space.

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.