Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Remove various Text Files with Date in Name

$
0
0

Greetings,

I am just staring my PowerShell Career and would like assistance in creating my first script. I have various programs (on various servers) that write text logs which use up a lot of disk space. The problem I have is that the only item in common is that all the logs us a date format. Some have text before and after the date. Example: Applog-yyyy-mm-dd.txt or [log.mm.dd.yyyy.xx.txt], or svc-yyyy-mm-ddlog.txt

I need to assistance in creating a script that will locate all .txt files with a date older than 30 days in the name and delete it.

Below is a start to the script however, I am unable to figure out how to add the "Date" in the name and filter by "Less Than" x date.

$date = (get-date).AddDays(-31).ToString("yyyy-MM-d")

Get-ChildItemC:\Test1\*.txt|Where {$_.Name -match"\d\-"} | Remove-Item -Confirm:$False -Recurse:$true

Thank you in advance for the help.


Viewing all articles
Browse latest Browse all 6937

Trending Articles