PowerShell: measuring size of the Windows TEMP directory
Posted by jpluimers on 2018/10/31
Due some issues in Windows, every now and then the Windows TEMP directory gets huge.
This script helps measuring the recursive size of that folder:
$WindowPath = [System.Environment]::GetFolderPath([System.Environment+SpecialFolder]::Windows)
$WindowTempPath = Join-Path -Path $WindowPath -ChildPath "TEMP"
$Result = Get-ChildItem $WindowTempPath -Recurse | Measure-Object -Property Length -Sum
$RecursiveSumInBytes = $Result.Sum
Write-Host "$RecursiveSumInBytes"
It uses these tricks:
- Accessing native .NET types; in this case [WayBack] Environment.SpecialFolder Enumeration (System) to get the “The Windows directory or SYSROOT. This corresponds to the %windir% or %SYSTEMROOT% environment variables. Added in the .NET Framework 4.”
- Assuming the Windows TEMP directory is always named that way.
- Using [WayBack] Join-Path to combine a base path with a child path without worrying about the path delimiter.
- Recursively enumerating all items in that folder using [WayBack] Get-ChildItem.
- Aggregating with [WayBack] Measure-Object over the
Lengthproperty of eachChild-Itemto determine theirSum.
After this, cleaning up uses two more tricks:
- the
Recurseoption of [WayBack] Remove-Item will remove both files and directories having files - the
FullNameproperty of theGet-Itemenumeration$_as otherwise you get the short name andRemove-Itemwill trying to delete those in the current directory (see [WayBack] PowerShell: Enumerate Files recursive and get full path for each file « Microsoft « ..::\ www.christiano.ch //::..).
$WindowPath = [System.Environment]::GetFolderPath([System.Environment+SpecialFolder]::Windows)
$WindowTempPath = Join-Path -Path $WindowPath -ChildPath "TEMP"
Get-ChildItem $WindowTempPath -Recurse | foreach { Remove-Item $_.FullName -Recurse }
Inspired by:
- [WayBack] Windows PowerShell Tip: Determining the Size of a Folder
- [WayBack] Determine directory size with PowerShell – Phil Erb which also has a nice trick to determine the best unit of measure (TB/GB/MB…)
–jeroen






Leave a comment