$cs.File_GetFolderSize
Description
Section titled “Description”Calculates the total size, in bytes, of every file under path, including all subfolders. Throws DirectoryNotFoundException if path doesn’t exist.
Syntax
Section titled “Syntax”$cs.File_GetFolderSize(string path)
Parameters
Section titled “Parameters”path (String)
Section titled “path (String)”Full path of the folder to measure.
Return value
Section titled “Return value”Long, the total size of the folder contents in bytes.
Example
Section titled “Example”$logFolder = $global:AppLogFolderif ($cs.File_ExistDir($logFolder)){ $sizeInBytes = $cs.File_GetFolderSize($logFolder) $cs.Job_WriteLog("Log folder size: $sizeInBytes bytes")}Sanity-check that an extraction step actually produced files, rather than silently leaving an empty folder:
$extractFolder = Join-Path $global:Packageroot "Extracted"if ($cs.File_ExistDir($extractFolder)){ $sizeInBytes = $cs.File_GetFolderSize($extractFolder) if ($sizeInBytes -eq 0) { Exit-PSScript -exitcode 1 -exitmessage "Extraction produced an empty folder, aborting install" }}Log how much space a data folder is using before removing it during cleanup:
$dataFolder = Join-Path $env:ProgramData "Contoso\Cache"if ($cs.File_ExistDir($dataFolder)){ $sizeInMb = [math]::Round($cs.File_GetFolderSize($dataFolder) / 1MB, 2) $cs.Job_WriteLog("Removing cache folder ($sizeInMb MB) at $dataFolder") $cs.File_DelTree($dataFolder)}