Script to remove files

Alex Buckland
Alex Buckland used Ask the Experts™
on
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.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
AlexA lack of information provides a lack of a decent solution.

Commented:
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.

Author

Commented:
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.
AlexA lack of information provides a lack of a decent solution.

Commented:
$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.
11/26 Forrester Webinar: Savings for Enterprise

How can your organization benefit from savings just by replacing your legacy backup solutions with Acronis' #CyberProtection? Join Forrester's Joe Branca and Ryan Davis from Acronis live as they explain how you can too.

Author

Commented:
Sorry same result, no output or error no change in files.... :-(
AlexA lack of information provides a lack of a decent solution.

Commented:
ARGH!

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

Thanks
Alex

Author

Commented:
Hi,

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

Thank you!
AlexA lack of information provides a lack of a decent solution.

Commented:
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"

Author

Commented:
sorry.....
screenshot attached....

Capture.PNG
AlexA lack of information provides a lack of a decent solution.

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

Run that, I know that works, there is no reason the other shouldn't.

Author

Commented:
Capture.PNG
AlexA lack of information provides a lack of a decent solution.

Commented:
You don't have access to the files beneath it

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

Thanks
Alex

Author

Commented:
I have full access, including read / write....

Capture.PNG
AlexA lack of information provides a lack of a decent solution.

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

Forgot the recurse which goes down levels

Author

Commented:
Bingo, that gives me output for the downloads folders....!
AlexA lack of information provides a lack of a decent solution.

Commented:
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

Author

Commented:
It does, yes.
AlexA lack of information provides a lack of a decent solution.

Commented:
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

Author

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

Capture.PNG

Author

Commented:
I put in 360 just in case. btw
AlexA lack of information provides a lack of a decent solution.
Commented:
get-childitem  D:\SchoolData\StaffData\ -filter Downloads -recurse | Get-ChildItem |  Where-Object {($_.LastWriteTime -lt (Get-Date).AddDays(-60))} | Remove-item -force -recurse -whatif

that should let you know what files it's going to remove, then you need to remove the -whatif switch
A lack of information provides a lack of a decent solution.
Commented:
oh please stipulate a profile on the  D:\SchoolData\StaffData\ as well to confirm it removes only the correct files.

Author

Commented:
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.
AlexA lack of information provides a lack of a decent solution.

Commented:
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

Author

Commented:
Very many thanks!
AlexA lack of information provides a lack of a decent solution.

Commented:
Sorry it took so long.

Author

Commented:
I had been trying this for two weeks, so dont worry!
AlexA lack of information provides a lack of a decent solution.

Commented:
oh, ok :-)

pleased I could help.

Thanks
Alex

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial