# Create folders and move files based on file creation date (month and year)

**URL:** <https://forums.powershell.org/t/create-folders-and-move-files-based-on-file-creation-date-month-and-year/23783>\
**Category:** PowerShell Help\
**Created:** [March 13, 2024, 8:05pm UTC](https://forums.powershell.org/t/create-folders-and-move-files-based-on-file-creation-date-month-and-year/23783 "2024-03-13T20:05:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![hip2b2](https://avatars.discourse-cdn.com/v4/letter/h/b2d939/32.png) [@hip2b2](https://forums.powershell.org/u/hip2b2)\
**Post date:** [March 13, 2024, 8:05pm UTC](https://forums.powershell.org/t/create-folders-and-move-files-based-on-file-creation-date-month-and-year/23783/1 "2024-03-13T20:05:26Z")

</div>

She who must be obeyed (my wife) has thousands of photos stored in a single pictures folder. She now realizes there is a better way and to get there would like to rationalize all the existing photos into individual folders based on photo creation date (yyyy-MM). She tells me the fix should be simple, so…

I have found the following script that should do what I want (but doesn’t):

> Blockquote  
> $rootpath = ‘C:\Users\ABC\Pictures’  
> $files = Get-ChildItem $rootpath -File  
> foreach($file in $files) {  
> $foldername = $file.CreationTime.ToString(‘yyyy-MM’)  
> $topath = “$rootpath$foldername”  
> if(-not(Test-Path $topath -PathType Container)){  
> New-Item $topath -ItemType Directory  
> }  
> Move-Item $file.FullName $topath  
> } \> Blockquote

The problem is that I have test run the scipt in different root folders with the same results, e.g., that only 2 subfolders are generated 2024-2, 2024-3, with pictures move into those folders.

I am stuck. Any help would be appreciated.

hip

---

<div class="post-metadata">

**Author:** ![neemobeer](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/neemobeer/32/4087_2.png) [@neemobeer](https://forums.powershell.org/u/neemobeer)\
**Post date:** [March 13, 2024, 9:29pm UTC](https://forums.powershell.org/t/create-folders-and-move-files-based-on-file-creation-date-month-and-year/23783/2 "2024-03-13T21:29:49Z")

</div>

Add a ‘-Recurse’ at the end of your Get-ChildItem cmdlet

```auto
$files = Get-ChildItem $rootpath -File
# should be
$files = Get-ChildItem $rootpath -File -Recurse

```

---

<div class="post-metadata">

**Author:** ![hip2b2](https://avatars.discourse-cdn.com/v4/letter/h/b2d939/32.png) [@hip2b2](https://forums.powershell.org/u/hip2b2)\
**Post date:** [March 13, 2024, 10:44pm UTC](https://forums.powershell.org/t/create-folders-and-move-files-based-on-file-creation-date-month-and-year/23783/3 "2024-03-13T22:44:11Z")

</div>

Work perfectly!

Thanks for the help.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [April 12, 2024, 10:44pm UTC](https://forums.powershell.org/t/create-folders-and-move-files-based-on-file-creation-date-month-and-year/23783/4 "2024-04-12T22:44:53Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
