How to rename your photos by the date taken using PowerShell?

Hasith Magage
3 min readJun 21, 2021

I was in a situation where I left my current job and I had to hand over my work laptop back. I wanted to backup my photos to a cloud storage before handing over my laptop. Before backing up I thought of organizing these photos properly and I though of writing a custom PowerShell script to match exactly my requirement.

My requirement was simply that I wanted to rename all my photos in the folders from the date they were taken. That way I can easily sort them by name (which is possible wherever I store them) and the photos can be seen as a timeline.

As an example, if I take the folder “Trip to Galle”, this folder had photos taken from many devices and they had different prefixes attached to the name of these photos which I wanted to get rid of.

I came up with this script to accomplish my requirement. Just run the below script from your parent photos folder and all the photos in the subfolders will be renamed accordingly. Now these photos are already sorted by the default sorting attribute ‘Name’ without using the advance sorting features of your operating system and the folders look very clean to me.

On a side note, this script is only written to get the job done within a short time so I have not given much attention to the coding standards.

In each photo, the data taken will be considered first. If data taken is not available, data modified will be taken instead as it is the closest one to date taken. Even if date modified is also not available, data created will be used for renaming.

12 — Date Taken
3 — Date Modified
4 — Date Created

$property = $folder.GetDetailsOf($file,12)if (-not $property) {    $property = $folder.GetDetailsOf($file,3)    if (-not $property) {        $property = $folder.GetDetailsOf($file,4)    }}

You can modified the file name format in the below statement.

$DateTaken = $datetime.ToString("yyyyMMdd_HHmm")

An iteration suffix will be added at the end of each photo if the file already exists.

while (Test-Path -Path $path -PathType Leaf) {    $iterator = $iterator + 1    $FileName = $DateTaken + "_" + $iterator    $path = $_.DirectoryName + "\" + $FileName + $_.Extension}

This is my first PowerShell script and the first article on Medium as well. So I apologize for any mistakes made.

I would suggest you to install Visual Studio Code with PowerShell extension for PowerShell scripting.

This might not be the best IDE but it is a good IDE to start.

Please feel free to take the script and customize as you want. Happy scripting!

Thank you.

--

--

Hasith Magage

Experienced Software Engineer | BEng (Hons) | OCPJP