<# .Synopsis Delete the specific object with the following parameters .Parameter -path path of the folder to look for the files ,this is MANDATORY .Parameter -retention_number_of_day Value for the number of day the files needs to be older to be deleted, this is MANDATORY .Parameter -pattern_to_delete Pattern to target if you need to delete a specific extension or pattern within the filename, this is OPTIONAL if you want to specify only the filename put it without any sign Example : -pattern_to_delete "catalina" if you want to target extension name specify the dot Example : -pattern_to_delete ".log" If you want to specify a specific filename and an extension, just add the * between the word Example : -pattern_to_delete "catalina*.log" .Parameter -recurse_boolean option to search recursively or not, this is OPTIONAL , DEFAULT = $false .Parameter -dry_run_boolean option to perform dry run and display what would be deleted this is OPTIONAL , DEFAULT = $false .Example # delete all the log files within the C:\Users\USER\Documents directory for the files older than 30 days with the name log within their name or extension deletion_of_specific_objects -path "C:\Users\USER\Documents" -retention_number_of_day 30 -pattern_to_delete "log" # delete all the files within the C:\Users\USER\Documents directory for all the files within all the folder deletion_of_specific_objects -path "C:\Users\USER\Documents" -retention_number_of_day 0 -recurse_boolean $true #> function deletion_of_specific_objects{ param( [Parameter (Mandatory = $true)] [String]$path, [Parameter (Mandatory = $false)] [String]$pattern_to_delete, [Parameter (Mandatory = $true)] [Int]$retention_number_of_day, [Parameter (Mandatory = $false)] [boolean]$recurse_boolean = $false, [Parameter (Mandatory = $false)] [boolean]$dry_run_boolean = $false ) $recurse_boolean_parameter = @{Recurse = $recurse_boolean} $Dry_run = @{Whatif = $dry_run_boolean} Get-ChildItem -Path $path/* -File -Include *$pattern_to_delete* @recurse_boolean_parameter |Where CreationTime -lt (Get-Date).AddDays(-$retention_number_of_day)|Remove-Item @Dry_run } ## deletion_of_specific_objects -path "C:\Users\USER\Documents" -retention_number_of_day 30 -pattern_to_delete "*.log" -recurse_boolean $true -dry_run_boolean $false