How to edit timestamps with Windows PowerShell

The following tutorial demonstrates how you can edit file timestamps -- create, last access and last write -- using Windows PowerShell.

PowerShell is part of any modern version of Microsoft's Windows operating system. Microsoft shifted to PowerShell away from the Command Prompt on Windows 10, but did not remove the command prompt doing so.

Windows 10 users and administrators have access to both, but the development focus lies clearly on PowerShell.

Each file on Windows, and other operating systems as well, has several timestamps associated with it. The file system keeps track of the files creation time, last access time, and last write time.

How to edit timestamps with Windows PowerShell

powershell change timestamp

First thing you need to do is to launch a PowerShell window.  Tap on the Windows-key, type PowerShell and hit the Enter-key afterwards. Note that this launches the prompt under the current user account. If you require an elevated prompt, for instance to edit file timestamps of folders that the user has limited access to, hold down Shift-key and Ctrl-key additionally.

The three commands that you require are the following ones:

  • $(Get-Item FILENAME.EXT).creationtime=$(DATE)
  • $(Get-Item FILENAME.EXT).lastaccesstime=$(DATE)
  • $(Get-Item FILENAME.EXT).lastwritetime=$(DATE)

The three commands change the creation, last access and last write timestamps of the file when you run them.

Note: Last Access Time is not enabled by default on all supported versions of Windows because of performance concerns.

To give you some examples:

  • $(Get-Item test.txt).creationtime=$(Get-Date)
  • $(Get-Item test.txt).lastaccesstime=$(Get-Date "12/24/2011 07:15 am")

The first command sets the creation timestamp of the file text.txt to the current date and time. The second command changes the last access time and date to December 24th, 2011 at 7:15 am.

Read also:  Windows 10 Cloud brings Windows RT back from the dead, sort of

Note that the command requires that the file is in the current directory of the PowerShell prompt.

Helpful commands

Once thing that may be useful is to list the file timestamps of the current folder before and after you run the PowerShell command. This makes it easier to find files that still require changing, and check whether the changes have been applied correctly.

Get-ChildItem -force | Select-Object Mode, Name, CreationTime, LastAccessTime, LastWriteTime | ft

powershell list timestamps

The command lists all files and folders of the current path, and displays the creation time, last access time and last write time of each item in a table.

  • -force in this context includes hidden and system files in the output.
  • ft is short for format table.

If you just need the create timestamp, run Get-ChildItem -force instead.

The following script runs the operation on all files.

$modifyfiles = Get-ChildItem -force | Where-Object {! $_.PSIsContainer}
foreach($object in $modifyfiles)
{
$object.CreationTime=("11/11/2011 12:00:00")

$object.LastAccessTime=("11/11/2011 12:00:00")

$object.LastWritetime=("11/11/2011 12:00:00")

}

Just copy and paste it, and change it according to your requirements.

Tip: If you prefer a graphical user interface when it comes to changing timestamps, check out the free software program Attribute Changer instead.

Summary
Article Name
How to edit timestamps with Windows PowerShell
Description
The following tutorial highlights how you can edit file timestamps -- create, last access and last write -- using Windows PowerShell.
Author
Publisher
Ghacks Technology News
Logo
Advertisement
Please share this article

Facebooktwittergoogle_plusredditlinkedinmail


Filed under:


Responses to How to edit timestamps with Windows PowerShell

  1. RossN October 9, 2017 at 10:56 am #

    Looks useful. I may be able to simplify a complex batch script I use to reset the date and time of an email archive that gets changed when emClient is closed.

  2. jupe October 9, 2017 at 11:10 am #

    note in the example given the missing end quote if it doesn't work for you

    "12/24/2011 07:15 am)

    should be

    "12/24/2011 07:15 am")

  3. MK October 16, 2017 at 9:33 am #

    And "12/24/2011 07:15" won't work for non-english versions (e.g. german) or might confuse month and date (e.g. canadian?).
    "2011-12-24 07:15 am" (or 07:15 pm) should work in all cases.

Leave a Reply