1

I need Powershell script to delete log files from log folder path older than 180 days, which can be performed using task scheduler.

I wrote this script, but it's not working.

$Path = "C:\Users\akshay.tanpure\Music\logs"
$Daysback = "-180"
$CurrentDate = Get-Date
$DatetoDelete = $CurrentDate.AddDays($Daysback)
Get-ChildItem $Path -Recurse -Include *.log | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item -Force
Akshay
  • 13
  • 4
  • 3
    Please be more descriptive than "it's not working" - is it throwing error messages? Deleting the wrong files? Deleting all the files? Are you sure any `*.log` files actually exist that haven't been written to in 180 days? – Mathias R. Jessen Jun 13 '23 at 11:18
  • Code looks correct, except that it should be `$Daysback = -180`. Make sure to configure the task to run as the currently logged on user, otherwise the script won't have access to the user profile directory. – zett42 Jun 13 '23 at 11:46
  • @MathiasR.Jessen It's not throwing any errors. After being scheduled in the Windows task scheduler, tasks are completed, but no .log files are deleted. – Akshay Jun 13 '23 at 13:19
  • @zett42 sure, will try with $Daysback = -180, As already configured the task to run as the currently logged on user. – Akshay Jun 13 '23 at 13:21

2 Answers2

1

What about

$Path = "C:\Users\akshay.tanpure\Music\logs"
$Daysback = "-180"
$CurrentDate = Get-Date
$DatetoDelete = $CurrentDate.AddDays($Daysback)
Get-ChildItem $Path -Recurse -filter *.log | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item -Force
Civette
  • 386
  • 2
  • 10
0

I've written and use similar on our servers. Editing your code to match the syntax I use successfully ends up with :

$Path = "C:\Users\akshay.tanpure\Music\logs"
$Daysback = -180
$DatetoDelete = (Get-Date).AddDays($Daysback).ToString("MM/dd/yyyy")
Get-ChildItem $Path -Recurse -Include *.log | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item -Force

As Zett42 noted, the -180 doesn't need to be in quotes, and in my case I found I needed to get the date information to the expected date format via ToString("MM/dd/yyyy")

Keith Langmead
  • 785
  • 1
  • 5
  • 16
  • 1
    No need to use `ToString()`. `Get-Date` produces a `[datetime]` object to compare with `LastWriteTime`. – lit Jun 13 '23 at 15:20
  • Ahh of course, in my case my script was first archiving the old files to .zip, had multiple logs per day, and I didn't want partial days, and wanted to use the date string later in the code. So since the delete works fine when passed a string that one line happened to tick all the boxes for multiple uses throughout the script. Nice bit of "I know this works, but can't remember why I did it this way" :) – Keith Langmead Jun 13 '23 at 16:08
  • Using `ToString()` without specifying a format will produce the timestamp according to the current regional settings. Better to leave `$DatetoDelete` as a `[datetime]`, then use `$DatetoDelete.ToString()` when it appears to the user. – lit Jun 13 '23 at 17:51