Script to delete old files from server

The script should delete the old files(files which are older than 30 days) from the server, and it should exclude those files whose name is starting with Evo.shibb.* and Hobsons.Platform.* and Shibboleth.*

It must delete only those files, whose name starts with connect.

I have tried writing the script as below,

  1. $limit = (Get-Date).AddMinutes(-5)
    $path = “C:\Users\mpradeep\Downloads\sample nuget apps”
    $Extension = “*.txt”
    [string]$Excludes = @(‘Evo.shib’, ‘Platform.m’, ‘Shibboleth.p’)

Get-ChildItem -Path $path -Exclude $Excludes -Filter $Extension -Force | Where-Object {$_.CreationTime -lt $limit} | Remove-Item

  1. Get-ChildItem . -Recurse -File -exclude “Evo.shib*” | Where CreationTime -lt (Get-Date).AddDays(-30) | Remove-Item -Force

Can anyone please help me on this, as I am new to Powershell.

I’m not sure what you mean by “it must only delete those files whose name starts with connect.” None of the exclusions you list start with “connect”, so do you want to exclude “Evo.shibb*”, “Hobsons.Platform*”, “Shibboleth*” or only delete “connect*”. Either way the approach is the same. Use get-childitem with the -exclude or -include switch depending on what you want to do, then pipe it to where-object and filter for only files older than 30 days, finally pipe that to remove-item. I would first use -whatif switch on remove-item before -force so you can be sure it is working as expected and not removing files you don’t want to. The -whatif will only output text telling you what would happen if you ran the command. Here is an example:

$Excludes = "Evo.shibb*", "Hobsons.Platform*", "Shibboleth*"
Get-ChildItem -Exclude $Excludes | ? -Property CreationTime -LT ((Get-Date).AddDays(-30)) | Remove-Item -WhatIf

In powershell, if there’s no * in the path (or no -recurse), using both -exclude and -filter will prevent any files from being listed. I consider this a bug. It still exists in PS 6 (actually not in 6.1pr2) . Beginners often catch these things. -include will have the same effect as -filter.

PS C:\Users\js> ls

    Directory: C:\Users\js

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        5/24/2018   4:48 PM              4 file.jpg
-a----        5/24/2018   4:43 PM              4 file.pdf
-a----        5/24/2018   4:42 PM              4 file.txt

PS C:\Users\js> Get-ChildItem -Path . -Filter *.txt -Exclude *.pdf
PS C:\Users\js> Get-ChildItem -Path .\* -Filter *.txt -Exclude *.pdf

    Directory: C:\Users\js

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        5/24/2018   4:42 PM              4 file.txt

hi mike,
Thanks for your reply on this post.

i just wanted to exclude those mentioned 3 files and also i wanted to delete only “connect.12.1.nukpg” (file name with some no. with nupkg extension), may be the approach is same instead of excluding those 3 files, i think we can directly include Connect related files and delete them. but still i need the script in that way as i mentioned, i was trying to do that, i didn’t get exact script.
In that folder or path there will be lot many files with different extension and filenames will starts with different names.

i hope you got my point !! please help me on this.

hey js,

yeah thanks for your approach.
i will give direct path to filter to my requirement.