This script allows you to get the disk space used by the Recycle Bin items on a shared folder.
It can be very useful and interesting if you run it on a file server where the user home drives are defined.

Script :

$sharedfolder='\\nas_server\home'
New-PSDrive -name "nasdrive" -PSProvider FileSystem -Root $sharedfolder

Get-ChildItem -Path "nasdrive:" -Force -Recurse -ErrorAction SilentlyContinue | `
	? {$_.name -match 'recycle.bin'} | `
	% {gci -Recurse $_.fullname  } | `
	% {$total += $_.length }
$total_in_MB=$total/1024/1024
write-host "The total size of the items located in the RecycleBin folders is : $total_in_MB MB"
Remove-PSDrive -Name "nasdrive"

References

Get-ChildItem

Get the items and child items in a folder or registry key. (dir / ls / gci)

Syntax
      Get-ChildItem [ [-path] string[] | [-literalPath] string[] ] 
         [[-filter] string] [-include string[]]  [-exclude string[]] [-name] 
            [-recurse] [-force] [-UseTransaction] [CommonParameters]

Key
   -path string
       The paths to the items from which content is to be retrieved.
       Wildcards are permitted. Default is the current directory (.)

   -literalPath string
       Like Path above, only the value is used exactly as typed.
       No characters are interpreted as wildcards. If the path includes any
       escape characters then enclose the path in single quotation marks.
        
   -include string
       Include only the specified items from the Path. e.g. 'May*'
        
   -exclude string
       Omit the specified items from the Path e.g. '*SS64*'
        
   -filter string
       A filter in the provider's format or language. 
       The exact syntax of the filter (wildcard support etc) depends on the provider.
       Filters are more efficient than -include/-exclude, because the provider
       applies the filter when retrieving the objects, rather than having 
       PowerShell filter the objects after they are retrieved.
	   
   -name 
       Retrieve only the names of the items.
       This is useful when piping the names of the child items to another command.
        
   -recurse 
       Get the items plus all child items of the location(s).
       Only for paths that point to a container such as C:\Windows or C:\Windows\*
       A path such as *.txt will not have any child items.

   -force
       Get all items including hidden or system files, but will not override
       security/file permissions.
        
   -UseTransaction
       Include the command in the active transaction.

   CommonParameters:
       -Verbose, -Debug, -ErrorAction, -ErrorVariable, -WarningAction, -WarningVariable,
       -OutBuffer -OutVariable.

Standard Aliases for Get-ChildItem: dir, list, ls, gci

When listing files and sub-directories, get-childitem will return the mode (attributes), last write time, file size (length), and the filename.
Valid modes (attributes) are: d (directory), a (archive), r (read-only), h (hidden), and s (system).

The default path is the current directory ‘ . ‘
To specify all the items in the current directory use ‘*’

Wildcards

When listing a single folder (without recursion), you can do get-childitem c:\music\*.mp3
Unlike the CMD shell, in PowerShell the path filter of c:\music\*.mp3 is applied only to files not folders (or other containers)
The way to apply a wildcard recursively to a whole tree of items in PowerShell is to use the -include parameter:
get-childitem c:\music\ -include *.mp3 -recurse

This change in syntax was required because some providers (such as the registry provider) allow backslashes in a value name, separating the -path from the -include string makes it possible to use get-childitem against any provider: files, registry, processes etc.

Get the disk space used by the Recycle Bin items

Leave a Reply

Your email address will not be published.