forked from tonypags/PsWinAdmin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemove-OldFile.ps1
More file actions
75 lines (62 loc) · 2.39 KB
/
Copy pathRemove-OldFile.ps1
File metadata and controls
75 lines (62 loc) · 2.39 KB
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
Function Remove-OldFile
{
<#
.SYNOPSIS
Deletes old files.
.DESCRIPTION
Deletes old files in a given folder which are older than a given date.
.EXAMPLE
Remove-OldFile -Path "C:\Program Files\Symantec\Symantec Endpoint Protection Manager\data\outbox\ImportPackage" -Retention 14 -whatif
#>
param (
# Folder where files in question are located
[Parameter(Mandatory=$true)]
[string]$Path,
# Number of Days old a file must be to be deleted
[Parameter(Mandatory=$true)]
[int]$Retention,
[Parameter()]
[switch]$WhatIf
)
### Get the drive space baseline value
$DriveLetter = (Resolve-Path $Path) -replace '\\.*'
$oDriveSpace = Get-WmiObject win32_logicaldisk |
where { $_.DeviceID -eq $DriveLetter } |
select -expand FreeSpace | %{
[math]::Round(( $_ / 1MB),0)
}
### BEGIN PERFORM ACTION ###
$FilesToDelete = Get-ChildItem -Path $Path |
Where {$_.Mode -notlike "*d*" } |
Where {$_.LastAccessTime -lt ((Get-Date).AddDays(-$Retention))}
if($WhatIf){$FilesToDelete | Remove-Item -WhatIf}
else{$FilesToDelete | Remove-Item -Force}
### END PERFORM ACTION ###
sleep 5
### Get the new drive space value
$pDriveSpace = Get-WmiObject win32_logicaldisk |
where { $_.DeviceID -eq $DriveLetter } |
select -expand FreeSpace | %{
[math]::Round(( $_ / 1MB),0)
}
### Find any files not deleted ###
$FilesNotDeleted = @()
$FilesToDelete | Foreach -Process {
$Item = $_
if (Test-Path $_.FullName){
$FilesNotDeleted += $Item
}
}
### END Find files not deleted ###
### BEGIN REPORT ###
if ($FilesNotDeleted){
Write-Output "$($FilesNotDeleted.count) of $($FilesToDelete.count) still exist! $($oDriveSpace - $pDriveSpace)MB of space recovered."
### To Do:
# notify the issue via email, inlcude client and device name as it appears on the nable alert email.
} else {
Write-Output "$($FilesToDelete.count) files deleted. $($oDriveSpace - $pDriveSpace)MB of space recovered."
### To Do:
# log files deleted
}
### END REPORT ###
}