OpsaC - Operating as PowerShell code
published: December 17, 2021 author: Tinu tags: PowerShell categories: PowerShell-Basic
We known a Windows Server has (too) many temp-folders and did not clean-up this folders.
List content of temp-folders if it’s older than 3 month.
$UsersRoot = $($home) -split '\\'
$AllUsers = Join-Path -Path "$($splitted[0])" -ChildPath "$($splitted[1])"
$AllUsersTemp = $AllUsers | Get-ChildItem | ForEach {
Join-Path -Path $_.FullName -ChildPath 'AppData\Local\Temp'
}
$folders = @(
"$($env:systemroot)\Temp"
$AllUsersTemp
)
Get-ChildItem $folders -ErrorAction SilentlyContinue | Sort LastWriteTime |
Where LastWriteTime -lt (Get-Date).AddMonths(-3)
Remove content of temp-folders if it’s older than 3 month.
$folders = @(
"$($env:systemroot)\Temp"
$AllUsersTemp
)
Get-ChildItem $folders -ErrorAction SilentlyContinue | Sort LastWriteTime |
Where LastWriteTime -lt (Get-Date).AddMonths(-3) | Remove-Item -Recurse
Weekend Scripter: Use PowerShell to Clean Out Temp Folders on devblogs.microsoft.com