-
Notifications
You must be signed in to change notification settings - Fork 0
/
DISK_Remove-ItemByAge.ps1
90 lines (67 loc) · 3.57 KB
/
DISK_Remove-ItemByAge.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
function remove-itembyage{
<#
.SYNOPSIS
remove items from folders recursively.
.DESCRIPTION
this function removes items older than a specified age from the target folder
.PARAMETER Days
Specifies the ammount of days since the file was last written to you wish to filter on.
.PARAMETER Path
Specifies the path to the folder you wish to search recursively.
.PARAMETER Silent
Instructs the function not to return any output.
.EXAMPLE
PS C:\> remove-itembyage -days 0 -path $recent
This command searches the $recent directory, for any files, then deletes them.
.EXAMPLE
PS C:\> remove-itembyage -days 5 -path $recent
This command searches the $recent directory, for files older than 5 days, then deletes them.
.EXAMPLE
PS C:\> remove-itembyage -days 10 -path $appdata -typefilter "txt,log"
This command searches the $cookies directory, for files older than 10 days and end with txt or log extensions, then deletes them.
.EXAMPLE
PS C:\> remove-itembyage -days 10 -path $cookies -typefilter "txt,log" -silent
This command searches the $cookies directory, for files older than 10 days and end with txt or log extensions, then deletes them without a report.
.NOTES
http://stealthpuppy.com/user-virtualization/profile-clean-up-script-powershell-edition/ for support information.
#Get KnownFolder Paths
$appdata=$env:appdata
$Cookies=(new-object -com shell.application).namespace(289).Self.Path
$History=(new-object -com shell.application).namespace(34).Self.Path
$recent=(new-object -com shell.application).namespace(8).Self.Path
$profile=$env:userprofile
#commands
remove-itembyage -days 0 -path $appdata -typefilter "txt,log" -silent -whatif
remove-itembyage -days 90 -path $cookies -silent -whatif
remove-itembyage -days 14 -path $recent -silent -whatif
remove-itembyage -days 21 -path $history -silent -whatif
remove-itembyage -days 14 -path "$appdata\Microsoft\office\Recent" -silent -whatif
.LINK
http://stealthpuppy.com/user-virtualization/profile-clean-up-script-powershell-edition/
#>
[cmdletbinding(SupportsShouldProcess=$True)]
param(
[Parameter(Mandatory=$true, Position=0,HelpMessage="Number of days to filter by, E.G. ""14""")]
[int]$days,
[Parameter(Mandatory=$true, Position=1,HelpMessage="Path to files you wish to delete")]
[string]$path,
[string]$typefilter,
[switch]$silent)
#check for silent switch
if ($silent){$ea="Silentlycontinue"}
Else {$ea="Continue"}
#check for typefilter, creates an array if specified.
if (!($typefilter)){$filter="*"}
Else{$filter=foreach ($item in $typefilter.split(",")){$item.insert(0,"*.")}}
if (test-path $path){
$now=get-date
$datefilter=$now.adddays(-$days)
foreach ($file in get-childitem "$path\*" -recurse -force -include $filter | where {$_.PSIsContainer -eq $false -and $_.lastwritetime -le $datefilter -and $_.name -ne "desktop.ini"}){
if (!($silent)){write-output "Deleting: $($file.fullname)"}
remove-item -literalPath $file.fullname -force -ea $ea
}#end for
}#end if
Else{
if (!($silent)){write-warning "the path specified does not exist! ($path)"}
}#end else
}#end function