Link to home
Start Free TrialLog in
Avatar of Michael Leonard
Michael LeonardFlag for United States of America

asked on

need a powershell script to delete files older than 3 days with a specific file name

can someone provide a script that I can use to delete files from the: C:\Windows\System32\winevt\Logs directory that start with the name:  Archive-Security-2015*

I would like to purge out anything older than 3 days.

thx in advance.

S.
Avatar of Marwan Osman
Marwan Osman
Flag of Lebanon image

Avatar of Michael Leonard

ASKER

Mar1, I can google as well, and I have already seen this link. the reason I am posting on this forum is for someone with powershell knowledge to provide a way for me to key on a specific name in the folder to be deleted, which isn't covered in this MSFT link that shows up first page in google.
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That script is "a bit" oversized for this purpose. The simple command is
$old = (Get-Date).AddDays(-3)
Get-ChildItem -recurse C:\Windows\System32\winevt\Logs -Include "Archive-Security-2015*" |
  ? { !$_.PsIsContainer -and $_.LastWriteTime -le $old} | 
  remove-item -whatif

Open in new window

This script is in test mode, and only displays what it would delete. Remove -WhatIf to let it happen.
thanks much, exactly what I was looking for oBda  ; )
Avatar of Ariel Funches
Ariel Funches

This should do the trick.

Get-ChildItem -Path 'C:\Windows\System32\winevt\Logs' -Recurse -ErrorAction SilentlyContinue -Force | Where-Object {! $_.PSIsContainer -And $_.LastWriteTime -le ([datetime]::Today).AddDays(-3)} | Remove-Item -Force -WhatIf

The -Whatif should be left in when you initially execute the script. It will display the files that it would delete had the script been executed "for real"; remove it once you get your desired output.

Enjoy!
Good eye, thank you, Qlemo.
Say (quick hijack, but related), does the -Include you used actually work for you?
gci C:\Windows\System32\winevt\Logs -Filter "Microsoft-*"

Open in new window

works as expected.
The following does not for me; the only "Include" that returns files that exist is "*", which somewhat defeats the point (on W2k8 R2, W2k12, PS 3 and 4)
gci C:\Windows\System32\winevt\Logs -Include "Microsoft-*"

Open in new window

thanks Qlemo and all, I had already assigned the points before you posted. appreciate the feedback, all great options
oBdA,

No, does not work for me either with -include, but with -filter.

I've reread my PS notes, and the recommendation is to use -Filter, as it is processed by the provider, and works with the current folder. It does not work with the Registry Provider, though, and only allows a single mask.
-Include and -Exclude are processed by PS itself, and only work (reliably) if used together with -Recurse. But they allow to provide a string array of masks.

In short, you were correct, but should have used better formatting :p
Siber1
Finding a script by searching google is not a bad idea especially when we provide a script from the technet gallery (we have applied from technet many scripts in our organization and there were very useful) so you can simply mention that the script in the link provided is not suitable for your case to have additional helps from other experts, no need to say words as if I made a mistake.

Marwan