Tinus EngOps Wiki

Logo

OpsaC - Operating as PowerShell code

Links

Home

IT-Wiki

PowerShell Blog

PowerShell Index

PowerShell Search

Additional Websites

View my GitHub Profile

View my GitHub Gists

View my Codeberg Page

View my Photo Website

Remove content from Temp

published: December 17, 2021 author: Tinu tags: PowerShell categories: PowerShell-Basic


Table of Contents

Temp-folders

We known a Windows Server has (too) many temp-folders and did not clean-up this folders.

List content from temp-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 from temp-folders

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

See also

Weekend Scripter: Use PowerShell to Clean Out Temp Folders on devblogs.microsoft.com


← Previous Post ◇ [ Top ] ◇ Copyright © 2024 by tinuwalther ◇ [ Blog ] ◇ Next Post →