Manage the file retention on a folder
Manage the file retention on a folder

With this script, you will be able to manage the file retention on a specific folder.
The file removal is based on age (LastWriteTime file property / source )

#Powershell Script to delete files older than a certain age
$intFileAge = 8  #age of files in days
$strFilePath = "C:\folder" #path to clean up

#create filter to exclude folders and files newer than specified age
Filter Select-FileAge {
	param($days)
	If ($_.PSisContainer) {}
		# Exclude folders from result set
	ElseIf ($_.LastWriteTime -lt (Get-Date).AddDays($days * -1)){$_}
}
get-Childitem -recurse $strFilePath | Select-FileAge $intFileAge 'CreationTime' |Remove-Item

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.

<>
Manage the file retention on a folder

Leave a Reply

Your email address will not be published.