Changing Files without Changing Modification Date

by Apr 22, 2013

Whenever you change a file, the file system automatically updates the LastWriteTime property. If you'd like to change a file without leaving such traces, try this:

# make sure this file and folder exist!!
$path = 'c:\temp\somefile.txt'

$file = Get-Item $Path
$lastModified = $file.LastWriteTime

# change file content:
"More Stuff" >> $Path

# replace modification date:
$file.LastWriteTime = $lastModified

As you can see, Get-Item returns a file object that gives you easy read/write access to file information such as LastWriteTime, CreationTime, or LastAccessTime. After the file is changed, the information is simply restored, and the updated file receives the old modification timestamp.

Twitter This Tip! ReTweet this Tip!