Created
January 31, 2018 18:08
-
-
Save BrendonKoz/e7c5415788febc7f93061f73b652fe90 to your computer and use it in GitHub Desktop.
A PowerShell script for deleting files (at logoff) from the Recycle Bin after X days of sitting in the bin.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ----------------------------------------------------------------------- | |
# | |
# Author : Baldwin D. | |
# Description : Empty Recycle Bin with Retention (Logoff Script) | |
# Source : http://baldwin-ps.blogspot.be/2013/07/empty-recycle-bin-with-retention-time.html | |
# | |
# ----------------------------------------------------------------------- | |
$Global:Collection = @() | |
$Shell = New-Object -ComObject Shell.Application | |
$Global:Recycler = $Shell.NameSpace(0xa) | |
$csvfile = "\\YourNetworkShare\RecycleBin.txt" | |
$LogFailed = "\\YourNetworkShare\RecycleBinFailed.txt" | |
function Get-recyclebin | |
{ | |
[CmdletBinding()] | |
Param | |
( | |
$RetentionTime = "7", | |
[Switch]$DeleteItems | |
) | |
$User = $env:USERNAME | |
$Computer = $env:COMPUTERNAME | |
$DateRun = Get-Date | |
foreach($item in $Recycler.Items()) | |
{ | |
$DeletedDate = $Recycler.GetDetailsOf($item,2) -replace "\u200f|\u200e","" #Invisible Unicode Characters | |
$DeletedDate_datetime = get-date $DeletedDate | |
[Int]$DeletedDays = (New-TimeSpan -Start $DeletedDate_datetime -End $(Get-Date)).Days | |
If($DeletedDays -ge $RetentionTime) | |
{ | |
$Size = $Recycler.GetDetailsOf($item,3) | |
$SizeArray = $Size -split " " | |
$Decimal = $SizeArray[0] -replace ",","." | |
If ($SizeArray[1] -contains "bytes") { $Size = [int]$Decimal /1024 } | |
If ($SizeArray[1] -contains "KB") { $Size = [int]$Decimal } | |
If ($SizeArray[1] -contains "MB") { $Size = [int]$Decimal * 1024 } | |
If ($SizeArray[1] -contains "GB") { $Size = [int]$Decimal *1024 *1024 } | |
$Object = New-Object Psobject -Property @{ | |
Computer = $computer | |
User = $User | |
DateRun = $DateRun | |
Name = $item.Name | |
Type = $item.Type | |
SizeKb = $Size | |
Path = $item.path | |
"Deleted Date" = $DeletedDate_datetime | |
"Deleted Days" = $DeletedDays } | |
$Object | |
If ($DeleteItems) | |
{ | |
Remove-Item -Path $item.Path -Confirm:$false -Force -Recurse | |
if ($?) | |
{ | |
$Global:Collection += @($object) | |
} | |
else | |
{ | |
Add-Content -Path $LogFailed -Value $error[0] | |
} | |
}#EndIf $DeleteItems | |
}#EndIf($DeletedDays -ge $RetentionTime) | |
}#EndForeach item | |
}#EndFunction | |
Get-recyclebin -RetentionTime $RetentionTime -DeleteItems #Remove the comment if you wish to actually delete the content | |
if (@($collection).count -gt "0") | |
{ | |
$Collection = $Collection | Select-Object "Computer","User","DateRun","Name","Type","Path","SizeKb","Deleted Days","Deleted Date" | |
$CsvData = $Collection | ConvertTo-Csv -NoTypeInformation | |
$Null, $Data = $CsvData | |
Add-Content -Path $csvfile -Value $Data | |
} | |
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($shell) | |
#ScriptEnd |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This code may require some adjusting. From a user comment on the source article page, I edited the code (line 78) for $RetentionTime so the variable only needs to be set once (at the top; it was set to 7 in both line 78 and line 23), but I'm not sure how variable scope works in PowerShell. If it's like other languages, the variable's value should be set outside of the function, not inside.