powershelldiskpart

PowerShell script to delete the Recovery Partition


I created a batch file that deletes the 'Recovery partition' but when I tried to create a PowerShell script, it says it cannot find the 'Recovery Partition'. Can anyone help me my code is below.

What I am trying to do is finding the 'Recovery partition' and then remove it so it is unallocated disk space. I was able to do this with a batch file but am having issues with PowerShell. We are not worried about this partition as we just deploy new machines.
This is a test we are doing. Thanks

    # Define logging setup
$logFolder = "C:\applog"
$logFile = "$logFolder\Diskpart_Log.txt"

# Ensure log folder exists
if (!(Test-Path -Path $logFolder)) {
New-Item -ItemType Directory -Path $logFolder -Force
}

# Function for logging
function Write-Log {
    param ($message)
    $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
    "$timestamp - $message" | Out-File -Append -FilePath $logFile
}

Write-Log "Script started. Identifying partitions..."


$diskpartListScript = "$logFolder\diskpart_list.txt"


@"
list disk
select disk 0
list partition
"@ | Out-File -FilePath $diskpartListScript


$partitionDetails = diskpart /s $diskpartListScript | Out-String
Write-Log "Retrieved partition details."


$primaryPartition = ($partitionDetails -match "Partition\s+\d+\s+\d+GB") -split "\s+" | 
Select-Object -Index 1
Write-Log "Primary partition identified: $primaryPartition"

# Identify the Recovery Partition ('Recovery' label)
$recoveryPartition = ($partitionDetails -match "Partition\s+\d+\s+\d+MB\s*Recovery") - 
split "\s+" | Select-Object -Index 1

if ($recoveryPartition) {
    
    $diskpartScript = "$logFolder\diskpart_script.txt"
    @"
    select disk 0
    select partition $recoveryPartition
    delete partition override
    "@ | Out-File -FilePath $diskpartScript

    Write-Log "Attempting to delete recovery partition #$recoveryPartition..."

 
    diskpart /s $diskpartScript | Out-File -Append -FilePath $logFile

    Write-Log "Recovery partition deletion complete."
    } else {
    Write-Log "No recovery partition found. Skipping deletion."
}

# Cleanup script files
Remove-Item $diskpartScript, $diskpartListScript -ErrorAction SilentlyContinue
Write-Log "Cleanup completed."
Write-Host "Script execution finished. Check log file at $logFile"

Thanks would appreciate the help


Solution

  • Use Get-Partition and Get-Volume instead of parsing diskpart. This will give you structured data. Updated your code but tough to test like this.

    Updated your code:

    # Define logging setup
    $logFolder = "C:\applog"
    $logFile = "$logFolder\Diskpart_Log.txt"
    
    # Ensure log folder exists
    if (!(Test-Path -Path $logFolder)) {
        New-Item -ItemType Directory -Path $logFolder -Force
    }
    
    # Function for logging
    function Write-Log {
        param ($message)
        $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
        "$timestamp - $message" | Out-File -Append -FilePath $logFile
    }
    
    Write-Log "Script started. Identifying recovery partition..."
    
    # Get all recovery partitions
    $recoveryPartitions = Get-Partition | Where-Object { $_.Type -eq 'Recovery' }
    
    if ($recoveryPartitions.Count -eq 0) {
        Write-Log "No recovery partition found. Skipping deletion."
    } else {
        foreach ($partition in $recoveryPartitions) {
            $diskNumber = $partition.DiskNumber
            $partitionNumber = $partition.PartitionNumber
    
            Write-Log "Found recovery partition on Disk $diskNumber, Partition $partitionNumber"
    
            $diskpartScript = @"
    select disk $diskNumber
    select partition $partitionNumber
    delete partition override
    "@
    
            $scriptPath = "$logFolder\diskpart_script.txt"
            $diskpartScript | Out-File -FilePath $scriptPath -Encoding ASCII
    
            Write-Log "Attempting to delete recovery partition..."
            diskpart /s $scriptPath | Out-File -Append -FilePath $logFile
            Write-Log "Recovery partition deletion complete."
            Remove-Item $scriptPath -ErrorAction SilentlyContinue
        }
    }
    
    Write-Log "Script execution finished."
    Write-Host "Check log file at $logFile"