Skip to content

Instantly share code, notes, and snippets.

@jongalloway
Created March 22, 2025 20:05
Show Gist options
  • Save jongalloway/fab5ee208caab21ec68d49fd44a55376 to your computer and use it in GitHub Desktop.
Save jongalloway/fab5ee208caab21ec68d49fd44a55376 to your computer and use it in GitHub Desktop.
# Define the dev drive letter and resizing percentage
$devDriveLetter = "D"
$resizePercentage = 10 # Set the percentage increase (e.g., 10%)
# Find the VHDX file associated with the dev drive
$mountedVHDs = Get-VHD | Where-Object -Property FileType -EQ "VHDX" | Where-Object -Property Attached -EQ $true
$devDriveVHD = $mountedVHDs | Where-Object {
$disk = Get-DiskImage -Path $_.Path | Get-Disk | Where-Object IsOffline -EQ $false
$partition = Get-Partition -DiskNumber $disk.Number | Where-Object DriveLetter -EQ $devDriveLetter
$partition -ne $null
}
if (-not $devDriveVHD) {
Write-Host "No VHDX file found for drive $devDriveLetter. Ensure the dev drive is backed by a VHDX file."
exit
}
# Output the path of the found VHDX file
$vhdxPath = $devDriveVHD.Path
Write-Host "VHDX file for drive $devDriveLetter found: $vhdxPath"
# Calculate current usage and target size
$usedSpace = (Get-PSDrive -Name $devDriveLetter).Used
$additionalSpace = $usedSpace * ($resizePercentage / 100)
$newPartitionSize = $usedSpace + $additionalSpace
# Convert the new size to bytes (rounded to GB)
$newPartitionSizeGB = [math]::Ceiling($newPartitionSize / 1GB) * 1GB
# Check VHDX size and resize if needed
$vhdxInfo = Get-VHD -Path $vhdxPath
if ($vhdxInfo.Size -lt $newPartitionSizeGB) {
Resize-VHD -Path $vhdxPath -SizeBytes $newPartitionSizeGB
Write-Host "Resized VHDX file to accommodate the new partition size: $newPartitionSizeGB bytes"
} else {
Write-Host "VHDX file already has sufficient capacity."
}
# Resize the partition
Resize-Partition -DriveLetter $devDriveLetter -Size $newPartitionSizeGB
Write-Host "Resized partition on drive $devDriveLetter to $newPartitionSizeGB bytes."
# Unmount the VHDX file
Dismount-DiskImage -ImagePath $vhdxPath
Write-Host "Unmounted VHDX file: $vhdxPath"
Write-Host "Process completed successfully."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment