PowerShell: How to Change the Modified Date of File


You can use the following syntax in PowerShell to change the modified date of a file:

(Get-Item "c:\users\bobbi\teams.txt").LastWriteTime = (Get-Date)

This particular example changes the modified date of the file named teams.txt to the current date and time.

Note that you could also use the following syntax to change the modified date of a file to a specific date and time:

(Get-Item "c:\users\bobbi\teams.txt").LastWriteTime = "10/18/2023 08:00:11"

This particular example changes the modified date of the file named teams.txt to the date and time that we specified.

The following example shows how to use each of these methods in practice.

Example: How to Change Modified Date of File in PowerShell

Suppose we have a file named teams.txt saved in the following location on our computer:

  • c:\users\bobbi\teams.txt

We can use the following syntax to view the last modified date of this file:

(Get-Item "c:\users\bobbi\teams.txt").LastWriteTime

The following screenshot shows how to use this syntax in practice:

We can see that this file was last modified on June 26, 2024 at 8:26:09 AM.

We can use the following syntax to change this last modified date to the current date and time when this article is being written:

(Get-Item "c:\users\bobbi\teams.txt").LastWriteTime = (Get-Date)

The following screenshot shows how to use this syntax in practice:

PowerShell change last modified date

We can see that this changes the last modified date of the file to July 3, 2024 at 10:24:45 AM, which represents the current date and time when this article is being written.

Note that we could also use the following syntax to change the last modified date and time to a specific date and time instead:

(Get-Item "c:\users\bobbi\teams.txt").LastWriteTime = "10/18/2023 08:00:11"

The following screenshot shows how to use this syntax in practice:

PowerShell change last modified date to specific date and time

We can see that this changes the last modified date of the file to October 18, 2023 at 8:00:11 AM, just as we specified.

Related Tutorials

The following tutorials explain how to perform other common tasks in PowerShell:

PowerShell: How to Get Most Recently Modified File in Directory
PowerShell: Get List of Files Modified After Certain Date
PowerShell: Check if File Has Been Modified in last 24 Hours

Leave a Reply