Have you ever needed to change the date or timestamp of a file or folder in Windows?

Windows maintains three different timestamps for every file and folder: Date Created, Date Modified, and Date Accessed. In most everyday scenarios, you never need to think about these values - Windows handles them automatically. However, there are situations where you may need to change the timestamp of a file or folder manually - for instance, when testing backup synchronization with Robocopy, organizing archived files, or correcting timestamps after a timezone migration.

PowerShell makes this straightforward. The commands below are the ones I reach for most often when I need to view or change file and folder timestamps.

Understanding file timestamps

Understanding file timestamps starts with knowing what each one represents:

TimestampDescription
CreationTimeThe date and time the file or folder was originally created
LastWriteTimeThe date and time the file or folder was last modified
LastAccessTimeThe date and time the file or folder was last accessed or read
File and folder timestamp properties in Windows

These properties are stored as part of the file system metadata and can be viewed in File Explorer under the file properties dialog, or retrieved using PowerShell.

View timestamps for all files in a folder

Viewing timestamps for all files in a folder is useful both before and after making changes. Use Get-ChildItem combined with Select-Object to display the relevant properties:

# Description: Displays the name and timestamps of all files in a folder
# Elevation is not required - read-only operation

Get-ChildItem -Force "D:\Test\" | Select-Object Name, CreationTime, LastWriteTime, LastAccessTime

View timestamps for all files in a folder

This command produces a clean table output showing the filename alongside each timestamp value:

Name     CreationTime          LastWriteTime         LastAccessTime
----     ------------          -------------         --------------
log1.txt 08/03/2019 17:00:00   08/03/2019 17:10:00   08/03/2019 17:10:00
log2.txt 08/03/2019 17:00:00   08/03/2019 17:10:00   08/03/2019 17:10:00
log3.txt 08/03/2019 17:00:00   08/03/2019 17:10:00   08/03/2019 17:10:00
log4.txt 08/03/2019 17:00:00   08/03/2019 17:10:00   08/03/2019 17:10:00

Example output

Change the timestamp of a single file

To change the timestamp of a single file, use the Get-Item cmdlet to reference the file and then set the desired property directly.

Change the creation date

# Description: Changes the creation timestamp of a specified file
# Elevation is not required - file ownership/permissions may apply

(Get-Item "D:\Test\log1.txt").CreationTime = ("3 August 2019 17:00:00")

Change the creation date of a single file

Change the last modified date

# Description: Changes the last write timestamp of a specified file
# Elevation is not required - file ownership/permissions may apply

(Get-Item "D:\Test\log1.txt").LastWriteTime = ("3 August 2019 17:10:00")

Change the last modified date of a single file

Change the last accessed date

# Description: Changes the last access timestamp of a specified file
# Elevation is not required - file ownership/permissions may apply

(Get-Item "D:\Test\log1.txt").LastAccessTime = ("3 August 2019 17:10:00")

Change the last accessed date of a single file

Change the timestamp of all files in a folder

To change the timestamp of all files in a folder, combine Get-ChildItem with ForEach-Object to iterate through each item and set the desired property.

Change all files and subfolders

The following commands change the timestamp for every file and subfolder in the specified directory:

# Description: Changes timestamps for all items in a folder including subfolders
# Elevation is not required - file ownership/permissions may apply

Get-ChildItem -Force "D:\Test\" * | ForEach-Object { $_.CreationTime = ("3 August 2019 17:00:00") }
Get-ChildItem -Force "D:\Test\" * | ForEach-Object { $_.LastWriteTime = ("3 August 2019 17:10:00") }
Get-ChildItem -Force "D:\Test\" * | ForEach-Object { $_.LastAccessTime = ("3 August 2019 17:10:00") }

Change timestamps for all files and subfolders

Change only files, excluding subfolders

If you want to apply the change only to files and exclude subfolders, add a Where-Object filter using the PSIsContainer property:

# Description: Changes timestamps for files only in a folder, excluding subfolders
# Elevation is not required - file ownership/permissions may apply

Get-ChildItem -Force "D:\Test\" * | Where-Object { !$_.PSIsContainer } | ForEach-Object { $_.CreationTime = ("3 August 2019 17:00:00") }
Get-ChildItem -Force "D:\Test\" * | Where-Object { !$_.PSIsContainer } | ForEach-Object { $_.LastWriteTime = ("3 August 2019 17:10:00") }
Get-ChildItem -Force "D:\Test\" * | Where-Object { !$_.PSIsContainer } | ForEach-Object { $_.LastAccessTime = ("3 August 2019 17:10:00") }

Change timestamps for files only, excluding subfolders

The -Force parameter ensures that hidden files are included in the operation.

Change the timestamp of a folder

Changing the timestamp of a folder itself works the same way as a file. Simply reference the folder path with a trailing backslash:

# Description: Changes the last write timestamp of a folder
# Elevation is not required - file ownership/permissions may apply

(Get-Item "D:\Test\").LastWriteTime = ("3 August 2019 17:00:00")

Change the last modified date of a folder

You can set CreationTime and LastAccessTime on folders the same way.

A few things to keep in mind

Changing file and folder timestamps is a simple operation, but there are a few things worth knowing before you start:

  • The date string format depends on your system’s regional settings. If "3 August 2019 17:00:00" does not work, try using a format like "2019-08-03 17:00:00" instead.
  • Changing timestamps does not modify the actual file content.
  • Some applications or services may reset the LastAccessTime after the change, depending on system configuration. Windows can be configured to disable last access time updates for performance reasons.
  • You need write permissions to the file or folder to change its timestamps. If you encounter an access denied error, try running PowerShell as an administrator.

–Jesper