FUNctions: Episode III, Revenge of the .LastWriteTime

Are you enjoying my Star Wars themed titles? Good. Because I’m running out of cleverness.

TL;DR – GitHub Repo

I wasted almost a hour my time the other day. No, I wasn’t doomscolling. I was PowerShelling. Here’s what happened…

Section 1 | Anecdotal Evidence of Stupidity

I was testing a copy script that keyed off of the LastWriteTime property: “all files with LastWriteTime greater than X copy to a directory”. When testing (in a real test environment!), I was puzzled at why my script wasn’t working. I was also swamped and not thinking clearly. So I did what anyone would do: overthink the problem. Instead of immediately identifying the slip up with LastWriteTime, I tested, modified my script, tested again, modified my script, tested a third time, and scratched my head for longer than I’d like to admit. Why wasn’t this working?! Then it hit me.

Section 2 | Over Engineering

Here’s a simple function that I hope someone can use, so they don’t run into the same simple issue that I did.

It works similarly to the “touch” command in Linux. It simply gets the file and updates the LastWriteTime property to that exact moment in time.

function Change-DateModified {
    param(
        [parameter(Mandatory=$true)]
            [string]$File
    )
    $Update = Get-Item -Path $File 
        $Update.LastWriteTime = Get-Date
}

Add this to your custom module now! Save yourself from feeling so stupid like I did. And if you have no idea what a custom module is, then check out my previous post regarding it.

As always!

This project is provided “as is” without any warranty of any kind, express or implied. Use it at your own risk. The authors and contributors are not responsible for any damage, data loss, or other issues that may arise from using this software. You are solely responsible for any actions taken based on this code.