Link to home
Create AccountLog in
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.
Avatar of Alex
Alex
Flag of United Kingdom of Great Britain and Northern Ireland image

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.
Avatar of Alex Buckland
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.
$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.
Sorry same result, no output or error no change in files.... :-(
ARGH!

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

Thanks
Alex
Hi,

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

Thank you!
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"
sorry.....
screenshot attached....

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

Run that, I know that works, there is no reason the other shouldn't.
You don't have access to the files beneath it

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

Thanks
Alex
I have full access, including read / write....

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

Forgot the recurse which goes down levels
Bingo, that gives me output for the downloads folders....!
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
It does, yes.
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
Bingo (almost) :-)
Can we remove this prompt?

User generated image
I put in 360 just in case. btw
SOLUTION
Avatar of Alex
Alex
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
ASKER CERTIFIED SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
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.
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
Very many thanks!
Sorry it took so long.
I had been trying this for two weeks, so dont worry!
oh, ok :-)

pleased I could help.

Thanks
Alex