enjay Posted October 10, 2017 Posted October 10, 2017 Save me reinventing the wheel, does anyone have a Powershell or Robocopy script which I could adapt to delete everything older than x months in everyone's Downloads folders? Ideally it would go through all the folders on a wildcard so I can just schedule it and forget, but I could import names into it if I had to.
GuyJD Posted October 10, 2017 Posted October 10, 2017 I found this which looks like it can be adapted, don't think it will delete folders in the directory though only files. https://www.thomasmaurer.ch/2010/12/powershell-delete-files-older-than/
enjay Posted October 10, 2017 Author Posted October 10, 2017 Thanks. Deleting sub-folders is less important in the Downloads folder, although there will be some from extracting ZIP files. I don't see from that script how I would edit it to run in everyone's Downloads folder though, unless you're thinking of setting it as a logon script.
GuyJD Posted October 10, 2017 Posted October 10, 2017 unless you're thinking of setting it as a logon script.yep that's how imagine it would have to work if your downloads folders are stored in user profiles, here I have the downloads folders all stored in a share on the server using folder redirection, so I could run it on the server to delete everything in everyone's download folder.
enjay Posted October 10, 2017 Author Posted October 10, 2017 Downloads folder is in the user profile on the server via folder redirection, \\servername\shares$\staff\jbloggs\downloads (MyDocs is in \\servername\shares$\staff\jbloggs\documents), so I was thinking of running the script across the server, telling it to go to \\servername\shares$\staff\[some variable to make it go in to every sub-folder]\downloads
robsonma Posted October 10, 2017 Posted October 10, 2017 (edited) Great Post, I was looking at this also, had a batch file, forfiles /p "\\Server\FolderRedirections$\%USERNAME%\My Documents\Downloads" /s /d -30 /c "cmd /c del @[u][url="http://www.edugeek.net/member.php?u=19139"]fil[/url][/u]e : date >= 30 days >NUL" but this didn't work also, i will keep working on it lol Edited October 10, 2017 by robsonma
GuyJD Posted October 10, 2017 Posted October 10, 2017 a way to do it might be to use the Get-ChildItem cmdlet to find the path to the downloads folder and then pipe them off into a file that you can then use to perform the Remove-Item script on.
enjay Posted October 10, 2017 Author Posted October 10, 2017 a way to do it might be to use the Get-ChildItem cmdlet to find the path to the downloads folder and then pipe them off into a file that you can then use to perform the Remove-Item script on. And that's the point at which my knowledge runs out. What would that command line look like?
GuyJD Posted October 12, 2017 Posted October 12, 2017 And that's the point at which my knowledge runs out. What would that command line look like?Unfortunately I've not had much chance to use powershell other than the odd script here and there to automate things since doing training on it 4 years ago, I've messed about with it for a bit this morning but I'm not getting very far with it, I can output the relative directory paths to a file using this script: $Path = "D:\Profiles" Get-ChildItem $Path -filter "Downloads" -directory -Recurse -name | out-file d:\output.txt But the merging that in with the script to delete the older files is where I can't get any further, I'll keep playing about with it and see if I can get it working as I'd quite like to use it too.
enjay Posted October 12, 2017 Author Posted October 12, 2017 That's a good step in the right direction though, as it would produce a list of all the users' Downloads path. That could then be pasted into an Excel sheet which is using some =concatenate functions to produce the command to delete them, which could then be pasted into a .ps1 file. So, not automated but does the job...
ADMaster Posted October 12, 2017 Posted October 12, 2017 You are going to want to use a nested foreach loop. The first one finds the downloads path for each profile. Assuming the path is D:\profile\Downloads The second one loops through every file in the downloads folder where the date is 90 days old. The first two action commands echos the full path to be deleted to screen and a text file. The third command that is commented out removes that path. I suggest running on a test copy of data first make sure the echoed output is what you want to deleted. There is also the -whatif command. dir is just an alias for Get-ChildItem you can change the 90 to how ever many days you want. When your ready to delete data for real un comment the remove item line. $profiles = dir d:\profiles $date = (get-date).AddDays(-90) foreach ($profile in $profiles){ $path = $profile.FullName + "\Downloads" $files = dir $path -Recurse | where {$_.LastWriteTime -lt $date} foreach ($file in $files ){ echo "$file.fullname will be deleted" echo "$file.fullname will be deleted" >> todelete.txt #Remove-Item $file -Recurse -Force }} 1
enjay Posted October 13, 2017 Author Posted October 13, 2017 Looks promising. So I change D:\Profiles in line one to the location of the staff shares, E:\Shares\StaffHome in my case, then let it do the rest, yes? Obviously I'll test first by pointing it to E:\Shares\StaffHome\enjay instead.
HPlum78 Posted October 13, 2017 Posted October 13, 2017 #### Script that finds all the files and folders in a directory older than xx number of days #### #### Enter the number of days to subtract ##### $NumberofDays = '32' #### Enter the path to search here #### $FldrPath = 'C:\Users\sa_o365sync\AppData\Local\Temp' $Time = (get-date).AddDays(-$NumberofDays) Get-ChildItem $FldrPath | Where-Object {$_.CreationTime -lt $Time} | Remove-Item -Recurse A slightly different take on @ADMaster script. To cover the "import names into it if I had to" you could use import-CSV \\FilesToDelete\FilesToDelete.csv and have a list of files that you want to delete in that file. and then pipe that into the remove-item cmdlet it would look like:- #### Script that finds all the files and folders in a directory older than x number of days #### #### Enter the number of days to subtract ##### $NumberofDays = '32' #### Enter the path to search here #### $FldrPath = 'C:\Users\sa_o365sync\AppData\Local\Temp' $Time = (get-date).AddDays(-$NumberofDays) Get-ChildItem $FldrPath | Where-Object {$_.CreationTime -lt $Time} | Remove-Item -Recurse #### Files to delete #### $Files = import-CSV \\FilesToDelete\FilesToDelete.csv #### Path to your files list $files | Remove-Item -Recurse I will add my general caveats around adding logging logic and that using write-host (echo) in scripts should be avoided (no one really runs scripts in the consoles). 1
ADMaster Posted October 13, 2017 Posted October 13, 2017 I'll test first by pointing it to E:\Shares\StaffHome\enjay instead. not quite, if you do that my script will look for a Downloads folder in every subfolder of your home drive. Make a copy of your drive and a few others. Lets say E:\Shares\StaffHomecopy Then my script will find E:\Shares\StaffHomecopy\enjay\Downloads There is probably a better way of doing it too but this is what I came up with. I will add my general caveats around adding logging logic and that using write-host (echo) in scripts should be avoided (no one really runs scripts in the consoles). Yes add logging, but I disagree about the console bit. I run scripts / and use echo from the console all the time. Particularly in the development and testing phase. I also run them in the console if they are one off scripts that are only run occasionally. If they are to be as a scheduled task then console output does no good. Example last week I was writing a script where the main action is to install a program but must meet certain conditions first. I'd put in a echo every stage of the if statements stating the current conditions with the install code commented out. This allowed me to work the bugs out a little easier. Also if your deploying a ps script with something like PDQ the echo statements will show up in your output log that pdq gives. In this case I can see the same results in the pdq console and a log file on the PC. Sorry for derailing a bit there.
enjay Posted October 13, 2017 Author Posted October 13, 2017 #### Enter the path to search here #### $FldrPath = 'C:\Users\sa_o365sync\AppData\Local\Temp' That's the sticking point - there are 1000 paths to search, so I need a script which will go and find those paths for me. I'm not trying to delete ALL files over 90 days, just the ones in E:\StaffShare\user1\downloads, E:\StaffShare\user2\downloads and so forth.
HPlum78 Posted October 13, 2017 Posted October 13, 2017 @ADMaster sorry was not having a dig, and you are correct write-host is a valuable tool when scripting a one shot script. I would say that where possible I would use write-output/ write-verbose as you can do something with the output rather than just limiting it to the console. I am very much in the camp of Jeff Snover and Don Jones on the use of write-host and as Don Jones says "Every time you use write-host a cat dies" What To Do / Not to Do in PowerShell: Part 1 | PowerShell with a Purpose Blog Write-Host Considered Harmful | Jeffrey Snover's blog
HPlum78 Posted October 13, 2017 Posted October 13, 2017 (edited) So I guess you have your users home folders set to something like E:\StaffShare\First.Last or E:\StaffShare\samAccountName or something along those lines.... so :- $NumberofDays = '32' $Time = (get-date).AddDays(-$NumberofDays) $UserNames = get-aduser -SearchBase "OU=Users,OU=StaffAccounts,DC=SCHOOL,DC=COM" -Filter * | Select samAccountName $UserNames | %{ $FldrPath = "E:\StaffShare\$($_)\downloads" Get-ChildItem $FldrPath | Where-Object {$_.CreationTime -lt $Time} | Remove-Item -Recurse } The above is one approach that you could use.... I would throw in a test-path and again some logging. Edited October 13, 2017 by HPlum78
ADMaster Posted October 13, 2017 Posted October 13, 2017 @HPlum78 Interesting reads, thanks. I probably fall into the last paragraph of the first link, that's the way it was done with VB and Batch. I'll consider using the other options but for debugging as I explained echo is just easy. 1
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now