eejit Posted October 16, 2019 Posted October 16, 2019 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: 4
Warwick_Tech Posted October 21, 2019 Posted October 21, 2019 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
eejit Posted October 25, 2019 Posted October 25, 2019 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. 3
computerguy Posted May 4, 2023 Posted May 4, 2023 sorry for the thread mine, is it possible to pass more than 1 directory to this script? I have 5 servers to reference in $path for recycle bin clearouts
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now