-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathClear-Temp.ps1
105 lines (82 loc) · 2.66 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<#
.SYNOPSIS
Clear the contents of TEMP, Windows\TEMP, and LocalAppData\TEMP.
.PARAMETER Quiet
Suppress any output; default is to report amount of disk space recovered.
#>
# CmdletBinding adds -Verbose functionality, SupportsShouldProcess adds -WhatIf
[CmdletBinding(
SupportsShouldProcess = $true,
DefaultParameterSetName = 'clear')]
param(
[switch] $Quiet,
[Parameter(ParameterSetName = 'Install')]
[switch] $Install = $false,
[Parameter(ParameterSetName = 'Uninstall')]
[switch] $Uninstall = $false
)
Begin
{
$script:taskName = 'Clear-Temp'
function ClearFolder
{
param($path)
if (!(Test-Path $path)) { return }
$fils = [System.IO.Directory]::GetFiles($path, '*').Count
$dirs = [System.IO.Directory]::GetDirectories($path, '*').Count
Write-Verbose "... clearing $path"
Remove-Item -Path "$path\*" -Force -Recurse -ErrorAction:SilentlyContinue
$fc = $fils - [System.IO.Directory]::GetFiles($path, '*').Count
$dc = $dirs - [System.IO.Directory]::GetDirectories($path, '*').Count
$script:filCount += $fc
$script:dirCount += $dc
if (!$Quiet)
{
Write-Host "... removed $fc files, $dc directories from $path" -ForegroundColor DarkGray
}
}
function RegisterTask
{
$user = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
$pwsh = 'powershell.exe' #[System.Diagnostics.Process]::GetCurrentProcess().Path
$log = Join-Path $env:USERPROFILE "task-logs\$taskName.log"
$command = "Start-Transcript $log; & '${PSCommandPath}'"
#$command = [Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes($command))
$action = New-ScheduledTaskAction -Execute $pwsh `
-Argument "-WindowStyle Hidden -ExecutionPolicy Bypass -Command ""${command}"""
$trigger = New-ScheduledTaskTrigger -Daily -At 5am
Register-ScheduledTask -TaskName $taskName -Action $action -Trigger $trigger -User $user -RunLevel Highest
}
function UnregisterTask
{
Unregister-ScheduledTask $taskName -Confirm:$false
}
}
Process
{
if ($PSCmdlet.ParameterSetName -eq 'Install')
{
RegisterTask
return
}
if ($PSCmdlet.ParameterSetName -eq 'Uninstall')
{
UnregisterTask
return
}
$used = (Get-PSDrive C).Used
$script:filCount = 0
$script:dirCount = 0
ClearFolder 'C:\Temp'
ClearFolder 'C:\Tmp'
ClearFolder 'C:\Windows\Temp'
ClearFolder (Join-Path $env:LocalAppData 'Temp')
if (!$Quiet)
{
$disk = Get-PSDrive C | Select-Object Used, Free
$pct = ($disk.Used / ($disk.Used + $disk.Free)) * 100
$recovered = $used - $disk.Used
Write-Host "... removed $filCount files, $dirCount directories"
Write-Host ("... recovered {0:0.00} MB on drive C, {1:0.00}% used" -f ($recovered / 1024000), $pct)
}
}