Jump to content

Recommended Posts

Posted

This script will recursively search through all sub-folders below the $path variable.

$Path = 'E:\Teaching Staff\Work'

$TotalBinSizes = 0
$TotalBins = 0
Get-ChildItem -Path $Path -Include '$Recycle.bin' -Recurse -Force -ErrorAction SilentlyContinue | ForEach-Object -Process {
   $TotalBins++
   $BinSize = (Get-ChildItem -Path $_.FullName -Force -Recurse -ErrorAction SilentlyContinue | Measure-Object -Property Length -Sum).sum
   
   '{0,-75} {1:N2} MB' -f $_.FullName, ($BinSize / 1mb)
   try
   {
       Remove-Item -Path $_.FullName -Confirm:$false -Force -Recurse -ErrorAction Stop
       $TotalBinSizes += $BinSize
   }
   catch
   {
       Write-Warning -Message 'Unable to empty bin.'
   }    
}
' '
if(($TotalBinSizes / 1mb) -gt 1024)
{
   $Size = $TotalBinSizes / 1gb
   $Unit = 'GB'
}
Else
{
   $Size = $TotalBinSizes / 1mb
   $Unit = 'MB'
}
if($TotalBins -gt 0)
{
   'There were {0} Recycled Bins emptied with a combined size of {1:N2} {2}.' -f $TotalBins, $Size, $Unit
}
Else
{
   'No Recycle Bins needed emptying.'
}

 

...and it looks like this:

rec1.png

  • Thanks 4
Posted
This script will recursively search through all sub-folders below the $path variable.

$Path = 'E:\Teaching Staff\Work'

$TotalBinSizes = 0
$TotalBins = 0
Get-ChildItem -Path $Path -Include '$Recycle.bin' -Recurse -Force -ErrorAction SilentlyContinue | ForEach-Object -Process {
   $TotalBins++
   $BinSize = (Get-ChildItem -Path $_.FullName -Force -Recurse -ErrorAction SilentlyContinue | Measure-Object -Property Length -Sum).sum
   
   '{0,-75} {1:N2} MB' -f $_.FullName, ($BinSize / 1mb)
   try
   {
       Remove-Item -Path $_.FullName -Confirm:$false -Force -Recurse -ErrorAction Stop
       $TotalBinSizes += $BinSize
   }
   catch
   {
       Write-Warning -Message 'Unable to empty bin.'
   }    
}
' '
if(($TotalBinSizes / 1mb) -gt 1024)
{
   $Size = $TotalBinSizes / 1gb
   $Unit = 'GB'
}
Else
{
   $Size = $TotalBinSizes / 1mb
   $Unit = 'MB'
}
if($TotalBins -gt 0)
{
   'There were {0} Recycled Bins emptied with a combined size of {1:N2} {2}.' -f $TotalBins, $Size, $Unit
}
Else
{
   'No Recycle Bins needed emptying.'
}

 

...and it looks like this:

[ATTACH=CONFIG]55357[/ATTACH]

 

This is a very elegant script - well done;

 

However it randomly fails with access denied errors on different users

 

Capture.JPG

Posted
I'm afraid that just means that you don't have the permissions to access those recycle bins. I've not come across that so it'll be difficult for me to play with the script and update it to list the bins that you need to get access to at the end, but if I do come across it I'll update this post again.
  • Thanks 3
  • 3 years later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...