-
Notifications
You must be signed in to change notification settings - Fork 0
/
Clear Temp.ps1
42 lines (32 loc) · 1.02 KB
/
Clear Temp.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
############################## GLOBALS ##############################
$global:debug = 0
$global:display = 'Normal'
$global:title = 'Clear Temp'
$global:args = $args
############################## VARIABLES ##############################
$folders = @(
"${PSScriptRoot}\tmp\",
"${PSScriptRoot}\temp\"
)
$daysToKeep = 1
$filesToKeep = 0
############################## MAIN CODE ##############################
function main {
if ($global:debug) { $global:args; '' }
if ($global:args.count -eq 0) {
$global:args = $folders
}
foreach ($arg in $global:args) {
if (Test-Path $arg -PathType 'Container') {
Get-ChildItem $arg -Recurse -Force | Where-Object { $_.LastWriteTime -lt (get-date).AddDays(-$daysToKeep) } | Sort-Object -Property LastWriteTime | Select-Object -SkipLast $filesToKeep | Foreach-Object {
if ($global:debug) { $_ | format-list }
Remove-Item $_.FullName -Force
}
}
}
if ($global:debug) { ''; pause }
exit
}
############################## RUN MAIN CODE ##############################
main