Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Powershell Script to Delete by File Name not working

$
0
0

Hi All,

Im having a problem with my powershell script to delete files by filename.

The File name consist of :FolderName_YYYY_MM_DD_#.log

There are hundreds of folders/sub folders

Example is 361_2019-05-21_09.log

E:\Folder1\361\361_2019-05-21_09.log.

What I am attempting to do is find the oldest file by file name and delete it and keep going through folders deleting files till 200GB is free, then I would set this up as a schedule to run once a day.

for some reason this script is not working for me:

$now = Get-Date
Get-ChildItem "E:\Logs -Filter '*.log' -Recurse|
foreach {
    if ($_.basename -match "*_(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})_*.log)$")
    {
        $fileData = Get-Date -Year $Matches.year -Month $Matches.month -Day $Matches.day
       
        $_ | Add-Member -MemberType NoteProperty -Name 'DateFromName' -Value $fileData -PassThru
    }
} | Where-Object {($_.DateFromName).addDays(-90) -lt $now} | Remove-Item -WhatIf -Force

is there anyone that can help


Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>