This quick blog post, shows, how you can sort and move files to folder sorted by date (year and month) with PowerShell. I had to sort a lot of files and put them into folders for each month and year. So for example when the files was created/modified in February 2012, the file had to be moved into the folder 2012 and the subfolder 2 (for February). For this, I created this quick and dirty script. This script gives you an example of how you can sort and move files by date using PowerShel.
# Get the files which should be moved, without folders $files = Get-ChildItem 'C:\Users\Thomas\OneDrive\OneDrive Camera Roll' -Recurse | where {!$_.PsIsContainer} # List Files which will be moved $files # Target Filder where files should be moved to. The script will automatically create a folder for the year and month. $targetPath = 'C:\Users\Thomas\OneDrive\pictures\Albums' foreach ($file in $files) { # Get year and Month of the file # I used LastWriteTime since this are synced files and the creation day will be the date when it was synced $year = $file.LastWriteTime.Year.ToString() $month = $file.LastWriteTime.Month.ToString() # Out FileName, year and month $file.Name $year $month # Set Directory Path $Directory = $targetPath + "\" + $year + "\" + $month # Create directory if it doesn't exsist if (!(Test-Path $Directory)) { New-Item $directory -type directory } # Move File to new location $file | Move-Item -Destination $Directory }
Please as always if you use a PowerShell script from the internet, test it first before you run it against your production environment. This script can for example also be very handy to sort documents, pictures or Windows Logfiles.
I hope this blog post helps you to sort and move files by date using PowerShell. If you have any questions, let me know in the comments below.
Tags: file, files, Folders, Microsoft, Move, Move Documents PowerShell, Move Files, Move Files PowerShell, Move Log Files, PowerShell, Sort, Sort Files, Windows Last modified: March 26, 2020
Fab works great, I want to add another subfolder for date 01, 02, 03 etc is this possible
Awesome! Thanks so much – very intuitive and a great intro to PowerShell. Not so scary now :-)
Works great…Thank you!
Good Job Thomas,.. Thanks for sharing this with us.
Thanks. Powershell is King.
Great article, makes me want to use PowerShell more often… Best greetings from the Lake of Constance ;-)
Hi thanks this was very help full can help me in moving files on the specif day example only monday files should be moved
Fantastic. Saved me hours!
I’ve added in the $day for it to sort the days also, is there a way that once you are sorting day, you can tell it to only sort from 2am Monday until 1:59AM Tuesday?
Hi, would love to use this, but on “date created” for photos, and creating a folder for year month only eg 2004 04
Thanks in advance
Thanks! Works great.
HI
Just what I was looking for however I have two quick questions, I am complete newbie to this, so aplogies if this has been asked before
1) how do I ensure all files from sub-folders in origin folder are included
2) what if it comes across a duplicate file, is there way to ensure it doesn’t overwrite file in destination folder but gives it a different name.
Hi Zee
1) This is done by the -recurse parameter
2) Well this script does not take care of that right now. This would be something which needs to be written. (additional functionality)
regards
Tom
Hi is it possible to sort by dateTaken ?
Now when i import movies from dec in jan it will sort it as the files was filmed in jan.
Otherwise, this script makes just what I’m looking for
Thanks for the code. I’ve made a project that advanced the functionality. Check it out on GitHub https://github.com/hoverdroids/Photo-Organizer
Hi,
Great Information thanks. Just a question. What if I want to move the files to the folders based on its creation years.
For example: The files creation date is 02/10/2017 the script should create the folder 2017 and move the file in the folder 2017. same if the file is created on 26/04/2018 the script should create the folder 2018 and move the file in the folder 2018.
Thanks in advance
This is very helpful, this saved so much time and all I just had to do was to add (Get-Date).DayOfWeek to generate files by day of the week and this script worked like magic. Thanks for sharing this script.
Thanks, glade the blog helped!
Glad I stumbled upon this – to sort all of my webcam images, vids, captures etc. Pimped it a bit to add the days too as subfolder, works like a charm – thanks a lot for it ;)
great it helped you :)
Nice bit of code. Did just what I needed after a small tweak.
glad you could use it :)
Thank you for the script. I used it and made enhancements on it to run multiple threads for moving a large set of files in parallel and look at the photos date using various ways (Date Take, or search for a date in the file or folder name).
The new code is here:
https://github.com/monahk/SortPhotos/blob/master/SortPhotos.ps1
Hi, The script is great. I’m using it to archive logs. I’m new to powershell and was wondering if there was a way to omit the last 2 months files?
Thanks for the script added this to omit the last 2 months:
$CurrentDate = Get-Date
$DaysBack = “-60”
$DateToMove = $CurrentDate.AddDays($DaysBack)
$files = Get-ChildItem ‘C:\Users\Thomas\OneDrive\pictures\Albums’ | Where-Object { $_.LastWriteTime -lt $DateToMove } | where {!$_.PsIsContainer}
I just wanted to say thanks for this. With a few slight mods it saved me hours of figuring things out and moved my photos to yyyy_mm_dd folders quickly.
Great that my blog post helped you! :)
Thank for this it works perfectly on Powershell for how i need things to work. But when I set up a schedule in task scheduler it doesnt execute, any clue why?
Does the user for the scheduled task have enough permissions?
Thanks very much
You’re welcome
I believe they do, Ive set up other successful tasks for other things the same way I’ve set up this one. It runs but I dont see any result, but I take the code put it into powershell, works like a charm.
I’m now ready to sort over 80000 photos scattered around my hard drive into something much more organised in the space of an hour or two. This will save me weeks of work. Thank you!!!!
Awesome to hear! :)
this solved my life today!!!!
happy to hear! :) thanks for the comment! :)
Hi Thomas, I was reviewing your webpage on line on PowerShell: Sort and Move Files by Date to month and year. I was trying to move a folder that has files sorted by date. I created daily folders of the year going back to 1977. I wanted to tailor your script to file these files in the appropriate folders. I.e. 2017-01-01 and put the files for that date in that folder, only in mass. I know you are a busy man, if it doesn’t take much to tweak your script. Thank you Thomas.
Actually I was able to duplicate and test it, my only issue left is to have my folders named “2020-11-22” etc Thanks Thomas
Thanks you very much, very helpful for my granpa photos
Still making an impact after 7 years of posting this. Exactly what I needed for my photo directory! Thanks 3000!
Thank you for the kind words and the great feedback! Happy it is useful!
Hello Thomas,
looks like lots of people still strive for sorting things around the world… Actually, me too! I have read many comments asking for a date classification in shape of yyyy/MM/dd.
I have studies something, adding the “day-value”
$year = $file.LastWriteTime.Year.ToString()
$month = $file.LastWriteTime.Month.ToString()
$day= $file.LastWriteTime.Day.ToString()
# Out FileName, year and month
$file.Name
$year
$month
$day
# Set Directory Path
$Directory = $targetPath + “\” + $year + “\” + $month + “\” + $day
but if I do this, days and months lack of the initial zeroes, and setting
$day= $file.LastWriteTime.Day.ToString(“dd”)
or
$day= $file.LastWriteTime.Day.ToString(‘dd’)
I get a “dd” subfolder…
Please, help us!
1- studied*
2- I managed in few hours accordingly to https://stackoverflow.com/questions/59612300/how-to-format-get-date-day-as-2-digits-in-powershell
as described in that article, when you handle dates in the ToString() format, dates become integers, so you have to put “00” in brackets both for months and days…
therefore, $day= $file.LastWriteTime.Day.ToString(“00”)
or, in case you need it, $day= $file.CreationTime.Day.ToString(“00”)