60

Is it possible to change a file or folders last modified date/time via PowerShell?

I have a folder folder1/ and I want to change the last modified date and time of that folder and it's contents via PowerShell.

5 Answers 5

60

Get the file object then set the property:

$file = Get-Item C:\Path\TO\File.txt
$file.LastWriteTime = (Get-Date)

or for a folder:

$folder = Get-Item C:\folder1
$folder.LastWriteTime = (Get-Date)
3
  • 4
    This can be done in one line, as (Get-Item C:\Path\To\File.txt).LastWriteTime = (Get-Date)
    – user7868
    Commented Jun 28, 2021 at 0:36
  • 1
    weird for folder doesn't seem to work
    – user75875
    Commented Nov 28, 2021 at 22:38
  • It seems like for folders recurse solutions below are necessary, and it won't apply to the "top level" folder.
    – Justin
    Commented Apr 12, 2023 at 14:03
28

The following way explained here works for me. So I used:

Get-ChildItem  C:\testFile1.txt | % {$_.LastWriteTime = '01/11/2005 06:01:36'}

Do not get confused by the "get-*" command... it will work regardless that it is a get instead of write or something. Keep also noted as written in the source that you need to use YOUR configured data format and maybe not the one in my example above.

1
  • 1
    That would be for all files within a folder. If it's just a single file, you could do (gci C:\testFile1.txt).LastWriteTime = '01/11/2005 06:01:36'. You can also replace the 'time' with Get-Date to dynamically set the current timestamp.
    – Blaisem
    Commented Feb 2, 2021 at 10:22
12

Yes, it is possible to change the last modified date. Here is a one liner example

powershell foreach($file in Get-ChildItem folder1) {$(Get-Item $file.Fullname).lastwritetime=$(Get-Date).AddHours(-5)}
1
  • 2
    This was useful for me thanks. The Get-ChildItem also has a -Recurse option so you can also do it for the entire tree.
    – Mendhak
    Commented Jun 13, 2020 at 9:49
9

To do a kind of unix touch in powershell on all files :

(Get-ChildItem -Path . –File –Recurse) | % {$_.LastWriteTime = (Get-Date)}

or all files and folders:

(Get-ChildItem -Path . –Recurse) | % {$_.LastWriteTime = (Get-Date)}
0

get-childitem d: \ * -recurse | % {$_.LastWriteTime = (Get-Date)}

This works for all files and folders in a tree.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .