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
|
Function Remove-File {
#-Recurse will search through all subfolders
#-hidden will the -force parameter for Get-ChildItem
#-force will use the -force parameter with Remove-Item
Param([string]$path=$env:temp,
[datetime]$cutoff=$(Throw "You must enter a cutoff date!"),
[Switch]$Recurse,
[Switch]$hidden,
[Switch]$force)
Write-Host "Removing files in $path older than $cutoff" -foregroundcolor CYAN
$cmd="Get-ChildItem $path"
if ($recurse) {
$cmd=$cmd + " -recurse"
}
if ($hidden) {
$cmd=$cmd + " -force"
}
#create an array to store file information
$files=@()
# execute the command string filtering out directories
&$executioncontext.InvokeCommand.NewScriptBlock($cmd) |
where {-not $_.PSIsContainer -and $_.lastwritetime -lt $cutoff} |
foreach {
#add current file to array
$files+=$_
if ($force) {
Remove-Item $_.fullname -force
}
else {
Remove-Item $_.fullname -force
}
} #end forEach
$stats=$files | Measure-Object -Sum length
$msg="Attempted to delete {0} files for a total of {1} MB ({2} bytes)" -f
$stats.count,($stats.sum/1MB -as [int]),$stats.sum
Write-Host $msg -foregroundcolor CYAN
}
$query="Select LastBootUpTime from Win32_OperatingSystem"
$boot=Get-WmiObject -query $query
[datetime]$boottime=$boot.ConvertToDateTime($boot.Lastbootuptime)
Remove-File $env:temp $boottime -recurse -hidden -force |
Partager