Skip to content

$cs.Sys_GetFreeDiskSpace

Returns the amount of free disk space, in megabytes, on the specified drive. Returns -1 if the drive isn’t ready or can’t be found.

$cs.Sys_GetFreeDiskSpace(string drive = “C:”)

Drive to get free disk space from. Defaults to "C:".

Double. Free disk space on the drive in MB, or -1 if the drive isn’t ready or can’t be found.

Log free space on the system drive for diagnostics before a large extraction:

Terminal window
$freeDiskspaceMB = $cs.Sys_GetFreeDiskSpace("C:")
$cs.Job_WriteLog("Free disk space on C: is $freeDiskspaceMB MB")

Check a non-system data drive before copying a large installation kit to it:

Terminal window
$freeDiskspaceMB = $cs.Sys_GetFreeDiskSpace("D:")
if ($freeDiskspaceMB -eq -1)
{
throw "Drive D: was not found"
}
$cs.Job_WriteLog("Free disk space on D: is $freeDiskspaceMB MB")

Compare free space before and after an installation step to see how much space it consumed:

Terminal window
$freeBeforeMB = $cs.Sys_GetFreeDiskSpace($global:gsSysDrive)
# ... run installer ...
$freeAfterMB = $cs.Sys_GetFreeDiskSpace($global:gsSysDrive)
$cs.Job_WriteLog("Install consumed $([math]::Round($freeBeforeMB - $freeAfterMB)) MB on $($global:gsSysDrive)")

$cs.Sys_IsMinimumRequiredDiskspaceAvailable