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.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Marwan OsmanCommented:
siber1Author Commented:
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.
Try this; it's in test mode and will only report on the files it would delete. Remove the -WhatIf argument at the end to run it for real:
Get-ChildItem -Path "C:\Windows\System32\winevt\Logs" -Filter "Archive-Security-2015*" | ? {$_.LastWriteTime -lt (Get-Date).AddDays(-3)} | Remove-Item -WhatIf

Open in new window

Edit: typo

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
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.
siber1Author Commented:
thanks much, exactly what I was looking for oBda  ; )
Ariel FunchesCommented:
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.

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

siber1Author Commented:
thanks Qlemo and all, I had already assigned the points before you posted. appreciate the feedback, all great options
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:

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
Marwan OsmanCommented:
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.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.