Shared folder recycle bin clean up

With this script you will be able to delete the content of the recycle bin of a network shared folder. It can be very useful if the shared folder contains the home directory for users.
To achieve this goal, it is important to use the PSDrive cmdlet.
In the example below, all items older than 60 days are removed.

$sharedfolder='\\nas-server\shared_folder'
New-PSDrive -name "nasdrive" -PSProvider FileSystem -Root $sharedfolder
 
Get-ChildItem -Path "nasdrive:" -Force -Recurse -ErrorAction SilentlyContinue | `
       ? {($_.fullname -match 'recycle.bin') -and ((Get-Date).AddDays(-60) -gt $_.LastWriteTime) -and ($_.PSIsContainer)} | `
       % {gci -Recurse -Force $_.fullname } | ? { ! $_.PSIsContainer } | `
       % {
	$a = $_.fullname
	$a = $a.replace($sharedfolder , 'nasdrive:')
	write-host $_.LastAccessTime"--"$_.LastWriteTime"--"$a
	$a | remove-item -force -recurse
	}
Remove-PSDrive -Name "nasdrive"  

Reference

New-PSDrive
Syntax

Parameter Set: Default
New-PSDrive [-Name]  [-PSProvider]  [-Root]  [-Credential  ] [-Description  ] [-Persist] [-Scope  ] [-Confirm] [-WhatIf] [-UseTransaction] [ ]

Detailed Description
The New-PSDrive cmdlet creates temporary and persistent drives that are “mapped” to or associated with a location in a data store, such as a network drive, a directory on the local computer, or a registry key, and persistent Windows mapped network drives that are associated with a file system location on a remote computer.

Temporary drives exist only in the current Windows PowerShell session and in sessions that you create in the current session. They can have any name that is valid in Windows PowerShell and can be mapped to any local or remote resource. You can use temporary Windows PowerShell drives to access data in the associated data store, just like you would do with any mapped network drive. You can change locations into the drive (using “set-location”, “cd”, or “chdir”) and access the contents of the drive (using “get-item”, “get-childitem”, or “dir”).

However, because temporary drives are known only to Windows PowerShell, you cannot access them by using File Explorer, Windows Management Instrumentation (WMI), Component Object Model (COM), or the Microsoft .NET Framework, or by using tools such as Net Use.

New features are added to New-PSDrive in Windows PowerShell 3.0.

— Mapped network drives: You can use the Persist parameter of New-PSDrive to create Windows mapped network drives. Unlike temporary Windows PowerShell drives, Windows mapped network drives are not session-specific; they are saved in Windows and they can be managed by using standard Windows tools, such as File Explorer and Net Use. Mapped network drives must have a drive-letter name and be connected to a remote file system location.

— External drives: When an external drive is connected to the computer, Windows PowerShell automatically adds a PSDrive to the file system that represents the new drive. You do not need to restart Windows PowerShell. Similarly, when an external drive is disconnected from the computer, Windows PowerShell automatically deletes the PSDrive that represents the removed drive.

— Credentials for UNC Paths: When the value of the Root parameter is a UNC path, such as \\Server\Share, the credential specified in the value of the Credential parameter is used to create the PSDrive. Otherwise, the Credential parameter is not effective when creating new file system drives.

Shared folder recycle bin clean up

2 thoughts on “Shared folder recycle bin clean up

  • January 20, 2017 at 12:10 pm
    Permalink

    Hi Nicolas,

    first: Thank you for this nice Script.
    I have the Problem, that the Script sometimes in hanging and waiting for something.
    Then I used ISE to debug and it tells me, we hung at:
    ? {($_.fullname -match ‘recycle.bin’) -and ((Get-Date).AddDays(-60) -gt $_.LastWriteTime) -and ($_.PSIsContainer)} | `

    Do you have any Idea how to add a timeout there?

    Best Regards
    Clemens

    Reply
    • January 30, 2017 at 3:13 pm
      Permalink

      Hello Clemens,

      As a first step of troubleshoot, you can increase the AddDays from -60 to -100 for example to reduce the file list. Keep me informed.

      Regards

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *