PowerShell: Expand Disk Volume to Maximum Size

$driveLetter='c'

function expandDiskMaxSize($driveLetter='c'){    
    try{
        $existingSize=(Get-Partition -DriveLetter $driveLetter).Size
        Update-HostStorageCache
        $max=(Get-PartitionSupportedSize -DriveLetter $driveLetter).SizeMax
        if($existingSize -lt $max){
            write-host "Resizing volume driver letter '$driveLetter' to its available maximum of $([math]::round($max/1GB))GB"
            Resize-Partition -DriveLetter $driveLetter -Size $max -ea Stop
        }else{
            write-host "Volume driver letter '$driveLetter' is not less than its available maximum of $([math]::round($existingSize/1GB))GB. Volume not resized."
        }
        return $true
    }catch{
        write-warning $_
        return $false
    }
}

expandDiskMaxSize $driveLetter

Leave a Reply

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