is there a way to get all the info of the recycle bin of multiple servers (name of file, size) and then later have an option to clear all the files inside it? I just want to know how much space I am deleting first. If possible I want to put all the server names in a txt file.

4 Spice ups

If you are using PowerShell 5.0 or higher, you can use the Clear-RecycleBin cmdlet. See: Clear-RecycleBin (Microsoft.PowerShell.Management) - PowerShell | Microsoft Learn for details.

As for querying the recycle bin, you could try this: https://gallery.technet.microsoft.com/scriptcenter/Get-RecycleBin-shows-the-085d12b0 although I have not tested it.

2 Spice ups

Take a peak at this. They have written a function to address querying of the Recycle bin.

https://www.jaapbrasser.com/clear-recyclebin-new-powershell-5-0-cmdlet-available-in-windows-10/

Thanks tfl and Brando_Blain. Im still in the query of recycle bin and attached is the script that I made (dont laugh please). Unfortunately, that script is querying the recycle bin of my laptop, not the servers in the txt file. Can you educate me on my mistake please?

get-recyclebin_test.txt (452 Bytes)

Yes, if you look at the code of Get-RecycleBin it only queries the local computer.

You would have to use Invoke-Command to query remote computers.

The same goes for clearing the recycle bin, the new cmdlet only works on the local computer.

M Boyle, thanks for the info. I believe I am so close. Please check the attached. I ran it and getting the error below

The term ‘Get-RecycleBin’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was
included, verify that the path is correct and try again.

  • CategoryInfo : ObjectNotFound: (Get-RecycleBin:String) , CommandNotFoundException
  • FullyQualifiedErrorId : CommandNotFoundException

get-recyclebin_invoke.txt (237 Bytes)

Closer :slight_smile:

You are getting the error because the module/function only exists on your local computer and not on the remote servers.

So you need to send it to the remote servers in some way, luckily it is quite small so no big deal. You can send the entire function to the remote server:

$sb = {
    function Get-RecycleBin 
    {
        (New-Object -ComObject Shell.Application).NameSpace(0x0a).Items() |
	        Select-Object Name, Size, Path
    }
    Get-RecycleBin
}
Invoke-Command -ComputerName $computer -ScriptBlock $sb 

Of course this won’t solve the problem of how to clean up the recycle bin as the remote servers will need PS 5 installed to user the cmdlet that Thomas mentioned.

M Boyle, that worked. Thank you so much! I’ll just find a way for the deletion of the files. One thing I noticed is this get-RecycleBin can only query the current user, it will not query the recyclebin of all the users in the server. I’ll just figure something out.