Welcome Forums General PowerShell Q&A Script to automatically delete files from the Recycle Bin

Viewing 5 reply threads
  • Author
    Posts
    • #165490
      Participant
      Topics: 1
      Replies: 1
      Points: 16
      Rank: Member

      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}
      }
    • #165544
      Senior Moderator
      Topics: 8
      Replies: 1160
      Points: 4,056
      Helping Hand
      Rank: Community Hero

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

    • #165607
      Participant
      Topics: 42
      Replies: 174
      Points: 628
      Helping Hand
      Rank: Major Contributor

      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
      }
    • #166211
      Participant
      Topics: 1
      Replies: 1
      Points: 16
      Rank: Member

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

      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
    • #166232
      Participant
      Topics: 4
      Replies: 54
      Points: 406
      Helping Hand
      Rank: Contributor
      
      $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
      }
      
      

      tweaked the code for local drives only

    • #166274
      Senior Moderator
      Topics: 8
      Replies: 1160
      Points: 4,056
      Helping Hand
      Rank: Community Hero

      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’

Viewing 5 reply threads
  • The topic ‘Script to automatically delete files from the Recycle Bin’ is closed to new replies.