-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRemove-ItemByDate.ps1
38 lines (34 loc) · 942 Bytes
/
Remove-ItemByDate.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
Function Remove-ItemByDate {
<#
.SYNOPSIS
Removes a file or folder older than a specified date
.DESCRIPTION
Takes any strings for the file name or extension.
.PARAMETER Item
The file or folder to be compared to
.PARAMETER Date
The date the the file or folder must be younger than
.EXAMPLE
Remove-ItemByDate -Item D:\JoeRod\joerod\Desktop\movies -Date (Get-Date).AddMonths(-2)
#>
[CmdletBinding(SupportsShouldProcess=$True)]
param(
[string]$Item,
[datetime]$Date
)
$args = @{}
if((Get-item $item).psiscontainer) {
$args.Recurse = $true
}
else {
$args.Recurse = $false
}
foreach($item_object in (Get-ChildItem $Item @args)){
if($item_object.CreationTime -lt $Date){
If ($PSCmdlet.ShouldProcess("Removing $($item_object.FullName)")) {
Remove-item -LiteralPath $($item_object.FullName) -Force -Confirm:$false @args
Write-Verbose "Removed $($item_object.FullName)"
}
}
}
}