Resetting Time Stamps via PowerShell

Sometimes when migrating a server or otherwise touching files on a system, the time stamps tend to get modified. (That is, after all, what they're there for).

However, in some situations, this is a sensitive field and some users may have to change they way they work if they sort by last modified in Windows Explorer.

As with most things PowerShell, Ed Wilson, The Scripting Guy, has us covered.



Function Set-FileTimeStamps {
# Taken from http://blogs.technet.com/b/heyscriptingguy/archive/2012/06/01/use-powershell-to-modify-file-access-time-stamps.aspx

 Param (

    [Parameter(mandatory=$true)]

    [string[]]$path,

    [datetime]$date = (Get-Date))

    Get-ChildItem -Path $path |

    ForEach-Object {

     $_.CreationTime = $creationtime

     $_.LastAccessTime = $accesstime

     $_.LastWriteTime = $modificationtime }

} #end function Set-FileTimeStamps

# Now, we can pass in a $newfilename after we've copied/moved/otherwise touched the file

    #Fix the timestamps
    Set-FileTimeStamps -path $newfilename

Comments

Popular Posts