, , , ,

PowerShell: Sort and Move Files by Date to month and year

PowerShell Sort and move files by date month year

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.

42 responses

  1. Kyle Avatar

    Fab works great, I want to add another subfolder for date 01, 02, 03 etc is this possible

  2. Tiffany Avatar

    Awesome! Thanks so much – very intuitive and a great intro to PowerShell. Not so scary now :-)

  3. Nandh Avatar

    Works great…Thank you!

  4. Ben Avatar

    Good Job Thomas,.. Thanks for sharing this with us.

  5. Matt Avatar

    Thanks. Powershell is King.

  6. David Avatar
    David

    Great article, makes me want to use PowerShell more often… Best greetings from the Lake of Constance ;-)

  7. Azhar Hussain Avatar
    Azhar Hussain

    Hi thanks this was very help full can help me in moving files on the specif day example only monday files should be moved

  8. Dan Avatar

    Fantastic. Saved me hours!

  9. Tom Avatar

    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?

  10. Dion Avatar

    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

  11. Pål Sletsjøe Avatar
    Pål Sletsjøe

    Thanks! Works great.

  12. Zee Avatar

    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.

    1. Thomas Maurer Avatar

      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

  13. Christopher Avatar
    Christopher

    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

  14. Chris Avatar

    Thanks for the code. I’ve made a project that advanced the functionality. Check it out on GitHub https://github.com/hoverdroids/Photo-Organizer

  15. kiran Avatar

    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

  16. Rakesh Avatar

    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.

    1. Thomas Maurer Avatar

      Thanks, glade the blog helped!

  17. Frank Avatar

    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 ;)

    1. Thomas Maurer Avatar

      great it helped you :)

  18. James Avatar

    Nice bit of code. Did just what I needed after a small tweak.

    1. Thomas Maurer Avatar

      glad you could use it :)

  19. Manah Avatar

    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

  20. Jenni Avatar

    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?

  21. Jenni Avatar

    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}

  22. Alex Avatar

    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.

    1. Thomas Maurer Avatar

      Great that my blog post helped you! :)

  23. Ches Avatar

    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?

    1. Thomas Maurer Avatar

      Does the user for the scheduled task have enough permissions?

  24. Naveen Balakrishnan Avatar
    Naveen Balakrishnan

    Thanks very much

    1. Thomas Maurer Avatar

      You’re welcome

  25. Ches Avatar

    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.

  26. Chris Avatar

    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!!!!

    1. Thomas Maurer Avatar

      Awesome to hear! :)

  27. Mauricio Avatar
    Mauricio

    this solved my life today!!!!

    1. Thomas Maurer Avatar

      happy to hear! :) thanks for the comment! :)

  28. Skip McCloskey Avatar
    Skip McCloskey

    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

  29. Fimeo Avatar

    Thanks you very much, very helpful for my granpa photos

  30. Rob Avatar

    Still making an impact after 7 years of posting this. Exactly what I needed for my photo directory! Thanks 3000!

    1. Thomas Maurer Avatar

      Thank you for the kind words and the great feedback! Happy it is useful!

  31. Andrea Avatar
    Andrea

    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!

  32. Andrea Avatar
    Andrea

    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”)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.