Follow sqlserver-dba.com

Subscribe to RSS feed  Follow @jackvamvas - Twitter

*Use the Comments section for questions

SQLServer-DBA.com Links

Dba_db2_button

dba-ninja.com

SQL Server DBA Jobs

Find Recycle Bin location and clear down with Powershell

12 April,2021 by Tom Collins

Question: I use Powershell extensively to manage SQL Server and the Windows OS. A common problem is to identify location of the  Windows Recycle Bin and clear the contents down - particuarly if there is a space issue. 

How can I locate the Windows Recycle Bin and clear it down?

 

Answer: This Powershell code will iterate through every local drive and identify if a Recycle Bin exists, and display the location. It's handy to iterate through all drives as the RecycleBin can be located on multiple drives . Typically - it's a hidden file so they can be configured across multiple drives

$Drives = get-wmiobject win32_volume | ? { $_.DriveType -eq 3 } | % { get-psdrive  $_.DriveLetter[0] }
foreach($Drive in $Drives) {
Get-ChildItem  $Drive':' -Directory -Hidden -Filter '$REC*' -Recurse -ErrorAction SilentlyContinue | ft Name,FullName
}

The code above focuses on identifying the location of the RecycleBin. Now we look at the Clearing down the Recycle Bin. Since Powershell v 5 - there is a new cmdlet called - Clear-RecycleBin.

An example of using this cmdlet is :

--Example 1: clear down a Recycle Bin from a sepecific drive letter,This method will prompt you for an action, such as :

Are you sure you want to perform this action?
Performing the operation "Clear-RecycleBin" on target "All of the contents of the Recycle Bin for the 'E:\' drive".
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): y

Clear-RecycleBin -DriveLetter D

--Example 2: clear down the Recycle Bin across all the local drives

Clear-RecycleBin -Force

 

If you want to automate the process of clearing down the Recycle Bin , then you can combine the the first code with the Clear-RecycleBin . BEWARE this will clear down all Recycle Bins across all the local drives

Example 3: combine identifying the local drives and clear down the Recycle Bin 

$Drives = get-wmiobject win32_volume | ? { $_.DriveType -eq 3 } | % { get-psdrive  $_.DriveLetter[0] }
foreach($Drive in $Drives) {
Get-ChildItem  $Drive':' -Directory -Hidden -Filter '$REC*' -Recurse -ErrorAction SilentlyContinue | ft Name,FullName
Clear-RecycleBin -DriveLetter $Drive -Force
}

 

--Open Powershell and type this command , to open the Recycle Bin 

start shell:RecycleBinFolder

 

Read More on Powershell 

Powershell Get-ChildItem with examples 


Author: Tom Collins (http://www.sqlserver-dba.com)


Share:

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment on Find Recycle Bin location and clear down with Powershell


sqlserver-dba.com | SQL Server Performance Tuning | SQL Server DBA:Everything | FAQ | Contact|Copyright & Disclaimer