2019-11-11 19:22:39 -05:00
|
|
|
[CmdletBinding()]
|
|
|
|
|
param(
|
2020-05-22 14:35:46 -04:00
|
|
|
[int]$OlderThan=7
|
2019-11-11 19:22:39 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
## Create a new timespan to compare with last write date of the target directory
|
|
|
|
|
$timedelta = New-TimeSpan -Days $OlderThan
|
|
|
|
|
|
|
|
|
|
## Remove all files and directories
|
|
|
|
|
Foreach ( $item in (Get-ChildItem -Path "${Env:WinDir}\TEMP") ) {
|
|
|
|
|
|
|
|
|
|
## If it's older than the number of days specified, recursively delete the directory
|
2020-05-22 14:35:46 -04:00
|
|
|
If ( $item.LastWriteTime -lt ((Get-Date) - $timedelta) ) { Remove-Item $item.FullName -Recurse -ErrorAction SilentlyContinue -Force }
|
2019-11-11 19:22:39 -05:00
|
|
|
|
|
|
|
|
}
|