This script will list the files in the Recycle bin, which are older than 100 days.

The recycle bin items are listed in the system where the script is running on.

ForEach ($DriveLetter in Get-PSDrive -PSProvider FileSystem) {
	$DelPath = $DriveLetter.Name + ':\$Recycle.Bin'
	$content = Get-ChildItem $DelPath -Force -Recurse -ErrorAction SilentlyContinue | `
		Where-Object { (Get-Date).AddDays(-100) -gt $_.LastWriteTime } 
	if ($content){
		$content |Get-Item



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

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

   -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.
       Retrieve only the names of the items.
       This is useful when piping the names of the child items to another command.
       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.

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

       -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 ‘*’


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.

List the files in the Recycle bin, which are older than 100 days

Leave a Reply

Your email address will not be published.