Avatar of Alex Buckland
Alex Buckland
 asked on

Script to remove files

Hi,

I was wondering if someone could help..... I have a windows 2012 fileserver I need to carry out some file deletion in mass on... I need a script that I can run periodically on the folder redirection store to delete files in staff "downloads" folders that are older than 60 days.

Expanding on this I may want to introduce a "last accessed" variable a little later on.

Any advice welcome.
Windows OSWindows Server 2012

Avatar of undefined
Last Comment
Alex

8/22/2022 - Mon
Alex

Get-ChildItem –Path "\\server\folder" -filter "Downloads" | Where-Object {($_.LastWriteTime -lt (Get-Date).AddDays(-60))} | Remove-Item

Open in new window




that should do it.
Alex Buckland

ASKER
Thank you.
I have tried that via powershell on just one users folder for now, but nothing happens. No errors or output, just back to prompt.
I tried both the UNC and local path same result.
Alex

$Downloads = Get-ChildItem –Path Driveletterforprofiles -filter "Downloads"
Foreach ($Download in $Downloads){ Get-ChildItem –Path $Download -recurse | Where-Object {($_.LastWriteTime -lt (Get-Date).AddDays(-60))} | Remove-Item -force}

Open in new window


that one should work.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Alex Buckland

ASKER
Sorry same result, no output or error no change in files.... :-(
Alex

ARGH!

Fine, what drive and folder are your profiles on? Also, you'll need to run powershell as administrator.

Thanks
Alex
Alex Buckland

ASKER
Hi,

The path to the redirections folder is.... D:\SchoolData\StaffData\
Yes running as an admin.....

Thank you!
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Alex

Stupid thing

needed a second get-childitem in there

get-childitem  D:\SchoolData\StaffData\ -filter Downloads | Get-ChildItem |  Where-Object {($_.LastWriteTime -lt (Get-Date).AddDays(-60))} | Remove-Item

Open in new window


Before you run this, run it like this

get-childitem  D:\SchoolData\StaffData\ -filter Downloads | Get-ChildItem |  Where-Object {($_.LastWriteTime -lt (Get-Date).AddDays(-60))} 

Open in new window


This will pull the old file list FIRST and then you can remove it when you confirm it's pulling the correct files.

Hope that no one is stupid enough to call a folder "Downloads"
Alex Buckland

ASKER
sorry.....
screenshot attached....

Capture.PNG
Alex

get-childitem  D:\SchoolData\StaffData\ -filter Downloads | Get-ChildItem

Run that, I know that works, there is no reason the other shouldn't.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Alex Buckland

ASKER
Capture.PNG
Alex

You don't have access to the files beneath it

Double click on a folder, does it prompt you to take permission?

Thanks
Alex
Alex Buckland

ASKER
I have full access, including read / write....

Capture.PNG
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Alex

get-childitem  D:\SchoolData\StaffData\ -filter Downloads -recurse | Get-ChildItem

Forgot the recurse which goes down levels
Alex Buckland

ASKER
Bingo, that gives me output for the downloads folders....!
Alex

Right

So that gives you the output of the folders.

Which means that this

get-childitem  D:\SchoolData\StaffData\ -filter Downloads -recurse | Get-ChildItem |  Where-Object {($_.LastWriteTime -lt (Get-Date).AddDays(-60))}

should give you all files that were last written after 60 days
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Alex Buckland

ASKER
It does, yes.
Alex

And as such

get-childitem  D:\SchoolData\StaffData\ -filter Downloads -recurse | Get-ChildItem |  Where-Object {($_.LastWriteTime -lt (Get-Date).AddDays(-60))} | Remove-item -force

Will then remove all the files, older than 60 days.

Which is EXACTLY what I.....Oh, I forgot the recurse on the first one.... I'm stupid, sorry. I'll get back in my box.

Regards

Alex
Alex Buckland

ASKER
Bingo (almost) :-)
Can we remove this prompt?

Capture.PNG
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Alex Buckland

ASKER
I put in 360 just in case. btw
SOLUTION
Alex

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER CERTIFIED SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Alex Buckland

ASKER
Perfect. Thank you so much for all your help (and patience). !
I am getting a few path too long errors but I not sure we can do anything about that.
Alex

Not really,

You'd need to go through and map the network drive or use robocopy to blow them away, but essentially you'll recoup a lot of space doing this.

Regards
alex
Your help has saved me hundreds of hours of internet surfing.
fblack61
Alex Buckland

ASKER
Very many thanks!
Alex

Sorry it took so long.
Alex Buckland

ASKER
I had been trying this for two weeks, so dont worry!
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Alex

oh, ok :-)

pleased I could help.

Thanks
Alex