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
Length
property of each Child-Item
to determine their Sum
.
After this, cleaning up uses two more tricks:
$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:
–jeroen
Like this:
Like Loading...