Script to automatically delete files from the Recycle Bin

Hi, I’m in the process of creating a script that will automatically delete content older than 30 days in all recycle bins on every hard drive except certain .ini files. But here I come across some errors when I’m trying to list the contents of the recycle bin with “Get-ChildItem”. In the following code you will find what I already have. Here is the first pure attempt to display a list of the contents.
For your information: I have been programming with Powershell for only 2 weeks. Before I had no contact to this language. Therefore, I thank you already once for your consideration.

$SID = (New-Object -ComObject Microsoft.DiskQuota).TranslateLogonNameToSID((Get-WmiObject -Class Win32_ComputerSystem).Username)
$Drive= Get-PSDrive | Select-Object -ExpandProperty 'Name' | Select-String -Pattern '^[a-z]$'
ForEach ($DNumber in $Drive){
$Path = $Drive.Name + '$Recycle.Bin' + "\$SID\"
$exclude = @('desktop.ini')
$test= Get-ChildItem $Path -Force -Recurse -Exclude $exclude | Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-30) }

Write-Host {$test}
}

Good start, can you update the thread with the error message you get .

Need to make a couple of changes…try this

$SID = (New-Object -ComObject Microsoft.DiskQuota).TranslateLogonNameToSID((Get-WmiObject -Class Win32_ComputerSystem).Username)
$Drives = Get-PSDrive -PSProvider FileSystem | Select -ExpandProperty root
ForEach ($Drive in $Drives){
$Path = $Drive + "`$Recycle.Bin" + "\$SID\"
$exclude = @('desktop.ini')
$test = Get-ChildItem $Path -Force -Recurse -Exclude $exclude | Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-30) }
Write-Host $test
}

[quote quote=165544]Good start, can you update the thread with the error message you get .

[/quote]
Hello, below you will find the error message which I receive. For information drive “D” is a CD drive in this case.

Get-ChildItem : Der Pfad "D:\$Recycle.Bin\S" kann nicht gefunden werden, da er nicht 
vorhanden ist.
In Zeile:6 Zeichen:9
+ $test = Get-ChildItem $Path -Force -Recurse -Exclude $exclude | Where ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (D:\$Recycle.Bin\S:String) [Get-ChildIt 
em], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItem 
Command

[pre]

$SID = (New-Object -ComObject Microsoft.DiskQuota).TranslateLogonNameToSID((Get-WmiObject -Class Win32_ComputerSystem).Username)
$Drives = get-wmiobject Win32_LogicalDisk | ? {$.drivetype -eq 3} | % {get-psdrive $.deviceid[0]}
ForEach ($Drive in $Drives){
$Path = $Drive.root + “`$Recycle.Bin” + "$SID"
$exclude = @(‘desktop.ini’)
$test = Get-ChildItem $Path -Force -Recurse -Exclude $exclude | Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-30) }
Write-Host $test
}

[/pre]

tweaked the code for local drives only

From a quick look, “D:$Recycle.Bin\S” will not be having the value you assume, whatever starts with $ is considered as a variable (here resulting in D:.Bin\S as $Recycle is not defined) when not escaped or not wrapped in single quotes, hence use single quotes

‘D:$Recycle.Bin\S’