Introduction
In this article, we will discuss how to extend the C: drive using PowerShell while leaving at least 1GB of unallocated space for the recovery partition. This is an essential process to ensure the system remains stable and can be easily restored in case of any system failures.
Prerequisites
Before proceeding, make sure you have the following:
- A working knowledge of PowerShell.
- Administrative privileges on the target machine.
- Disk Management tool installed.
Steps to Extend C: Drive
To extend the C: drive using PowerShell, follow the steps below:
Step 1: Identify the Unallocated Space
The first step is to identify the unallocated space next to the C: drive. Use the following PowerShell command:
$disk = Get-WmiObject Win32_LogicalDisk -Filter "DriveType -eq 3 - and DeviceID -eq 'C:'"
$disk | Select-Object IndexNumber, Size, FreeSpace
Step 2: Calculate the Required Unallocated Space
Calculate the required unallocated space for the recovery partition. In this example, we will leave 1GB of unallocated space:
$recoveryPartitionSize = 1GB
Step 3: Identify the Unallocated Space Next to C: Drive
Identify the unallocated space next to the C: drive:
$disk | ForEach-Object {
$_.FreeSpace
$adjacentFreeSpace = $_.FreeSpace - $_._Size
if ($adjacentFreeSpace -gt $recoveryPartitionSize) {
$adjacentDisk = Get-WmiObject Win32_LogicalDisk -Filter "IndexNumber -eq ($_.IndexNumber + 1)"
$adjacentDisk
break
}
}
Step 4: Extend the C: Drive
Extend the C: drive using the unallocated space:
$disk | ForEach-Object {
if ($_.IndexNumber -eq (Get-WmiObject Win32_LogicalDisk -Filter "DeviceID -eq 'C:'" | Select-Object IndexNumber).IndexNumber) {
Expand-Volume -VolumeLength (($_._Size + $adjacentFreeSpace) - $recoveryPartitionSize) -NoJustify -Confirm:$false
}
}
Summary
In this article, we learned how to extend the C: drive using PowerShell while leaving at least 1GB of unallocated space for the recovery partition. This process is essential to ensure the system remains stable and can be easily restored in case of any system failures.