Set Creation- and Modification-Date from Filename

After my return of my last vacation, I had a bunch of videos that I wanted to edit, transcode and archive. First I renamed the files based on their creation date so that this information is being preserved and consistent during the whole process.
When my editing and transcoding was done I just needed to set the file’s modification and creation date back to when it was originally taken. To do this job I created a small PowerShell Script I want to share with you:

$Directory = "C:PathtoFiles"
$DateFormat = "yyyy-MM-dd_HHmmss"

foreach ($file in (Get-ChildItem $Directory)) {
    $date_from_file=[datetime]::ParseExact($file.Name.Substring(0,$file.Name.Length-4), $DateFormat, $null)

    $file.CreationTime = $date_from_file
    $file.LastAccessTime = $date_from_file
    $file.LastWriteTime = $date_from_file

    Write-Host ($file.Name + " - " + $date_from_file)

Here’s the result – the date / timestamp in the filename is identical with the actual file date:

Set Creation- and Modification-Date from Filename

You may also like
Script-Gallery: Fix-Printer.ps1
PowerShell Best Practices: Set-StrictMode
Get running processes including CPU and memory usage
  • Andy
    May 25, 2020 at 12:31

    This is simply brilliant, in the truest sense of the term!

    Spent a whole day looking through different programs and apps and forums to see what could help me sort out my odd-format video files from the last decade – in the end, thanks to you, I can continue to use handbrake and, with a little help from Adobe Bridge, I’m good to go – thanks so much for sharing!

  • Mark
    September 2, 2017 at 00:58

    Thanks for this, saved me a lot of work today 🙂

Leave Your Comment

Your Comment*

Your Name*
Your Webpage