Use PowerShell to Modify File Access Time Stamps

Summary: Learn how to use Windows PowerShell to modify file creation and modification, and to access time stamps.

Hey, Scripting Guy! Question Hey, Scripting Guy! There are times that I would love to be able to manipulate file time stamps. I am talking about when they are created, changed, and accessed. I used to have a utility that did this for me on another operating system, but I have not been able to find something that will work with Windows PowerShell. Do you know of anything?


Hey, Scripting Guy! Answer Hello TK,

Microsoft Scripting Guy, Ed Wilson, is here. When I was writing all of the scripts for the Windows 7 Resource Kit, just before I uploaded everything to Mitch Tulloch, I would change the time stamps on all of the scripts. In this way, they all had the same time stamp, and it made for a simple type of version control. It was a simple command, and therefore, I did not write a script or function to do this.

Changing file attributes

The key to changing file attributes is two-fold. First, you must have permissions, and second, you need to realize that the attributes themselves are Read/Write. This second part is easy. Use the Get-Member cmdlet as shown here.

PS C:> Get-Item C:Changeda.ps1 | gm -Name *time

   TypeName: System.IO.FileInfo


Name           MemberType Definition

—-           ———- ———-

CreationTime   Property   System.DateTime CreationTime {get;set;}

LastAccessTime Property   System.DateTime LastAccessTime {get;set;}

LastWriteTime  Property   System.DateTime LastWriteTime {get;set;}

As shown here, three properties end with the word Time. In addition, all three properties appear as get;set, meaning that the values are both retrievable and settable. To assign a new value to an attribute, you only need to a straightforward value assignment. In the code that is shown here, I use the Get-Item cmdlet to retrieve basic information about a text file named a.txt.

PS C:> Get-Item C:fsoa.txt

    Directory: C:fso


Mode                LastWriteTime     Length Name

—-                ————-     —— —-

-a—         6/12/2007   1:55 PM       3502 a.txt

One way to change the LastWriteTime property is to store the FileInfo object in a variable, and then use the equals operator to assign a new value to the property. In the code that follows, the Get-Item cmdlet retireves the FileInfo object for the a.txt text file. Then I assign a new value to the LastWriteTime property. The new value is the current date and time retrieved via the Get-Date cmdlet. Finally, the basic properties of the file display.

PS C:> $a = Get-Item C:fsoa.txt

PS C:> $a.LastWriteTime = (get-date)

PS C:> Get-Item c:fsoa.txt

    Directory: C:fso

Mode                LastWriteTime     Length Name

—-                ————-     —— —-

-a—         5/31/2012  10:14 AM       3502 a.txt

Creating a function

To simplify the process of setting file time stamps, I created the following function. It accepts an array of file paths, and uses the current date and time for the new values. The Path parameter is a mandatory parameter. This portion of the function is shown here:

Param (



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

The main portion of the function uses the Get-ChildItem cmdlet to retrieve all files and folders in the current path. It does not use the Recurse switched parameter, but if you want to add it, you could. For my purposes, I do not want it to Recurse, so I left the switch off. Next, the objects pass to the Foreach-Object cmdlet, and the three time stamp properties change to the new value. Because the new date uses the [datetime] constraint, any value that Windows PowerShell interprets as a date/time value is acceptable. For example, the following command works on my system because Windows PowerShell is able to create a date from 7/1/11.

Set-FileTimeStamps -path C:Ref -date 7/1/11

The complete Set-FileTimeStamps function is shown here:

Set-FileTimeStamps function

Function Set-FileTimeStamps


 Param (



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

    Get-ChildItem -Path $path |

    ForEach-Object {

     $_.CreationTime = $date

     $_.LastAccessTime = $date

     $_.LastWriteTime = $date }

} #end function Set-FileTimeStamps

TK, that is all there is to using Windows PowerShell to modify time stamps on files.  Join me tomorrow when we have a guest blog from Mike Robbins that talks about using Windows PowerShell with backups. It is a cool blog.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy

Comments (7)

  1. Nirmal Jose says:

    Do you think it will affect timestamp for Digital forensic purpose. Does any log stored somewhere ?

  2. Ed Wilson says:

    @Nirmal Jose I do believe that it does in fact, modify the timestamp. The action will be logged however, if you have file auditing turned on. In addition, it may be logged in the Security log if you are auditing privilege use.

  3. justin says:

    You gave me the last piece to my puzzle. Just in case someone gets here with the problem I had, here’s a little script to go through all files and folders and if older than xxxx days, then change the specified dates and times. In my case I had a bunch
    of naughty of Macs writing files and folders with a date of Jan 24 1984 which just so happens to be the date the original Mac was released. Coincidence? I think not!

    I happen to not have any files that should be older than 1999, the date my first storage server when into service. So you might need/want to tweak that number (5475) a little based on your needs.

    $a = Get-ChildItem "TARGET FOLDER" -Recurse -Force
    foreach($x in $a)
    if (((Get-Date) – $x.CreationTime).Days -gt 5475)
    $x.CreationTime = (Get-Date)
    $x.LastAccessTime = (Get-Date)
    $x.LastWriteTime = (Get-Date)

    Many thanks Ed!!!!

  4. omsft says:

    Here’s a variation on this. To replace the Created Date with the Modified Date of all files in a folder:

    Get-ChildItem -Path | ForEach-Object { $_.CreationTime = $_.LastWriteTime }

  5. JB in MN says:

    @Justin, you could also do the following so that the comparison doesn’t have to change all the time.

    $target = get-date "1/1/1999 12:00:01 AM"
    if ($x.CreationTime -lt $target){…}

  6. Antonio Gil says:

    Good evening,

    First, let me thank you for this awesome site, it has helped me a lot to understand how to do some things on powershell.

    About this function, I currently trying to adapt it to change the TimeStamps for a single file (which filename I intend to provide), and such file will be on the same directory where I’ll run the function.

    So far (and as I was thinking that will work) the script goes as follows

    Function Set-FileTimeStamps
    Param (
    [datetime]$date = (Get-Date))
    $file.CreationTime = $date
    $file.LastAccessTime = $date
    $file.LastWriteTime = $date
    } #end function Set-FileTimeStamps

    And it keeps me telling me that:

    Property ‘CreationTime’ cannot be found on this object; make sure it exists and is settable.
    At C:UsersAntonDocumentsWindowsPowerShellModulesSet-FileTimeStampsSet-FileTimeStamps.psm1:7 char:11
    + $file. <<<< CreationTime = $date
    + CategoryInfo : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyAssignmentException

    So, any idea about in which part I’m messing up the script? Because, I’m lost as hell.

    Thanks in advance!

  7. AB says:

    @Antonio Gil The file variable in $file.CreationTime is not a FileInfo Object. For clarification review the paragraph starting ‘One way to change…’ You may need something like:
    $fiObj = (Get-Item $file)
    then you can do


Skip to main content