Use PowerShell to Modify File Access Time Stamps

Doctor Scripto

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? —TK 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


Discussion is closed.

Feedback usabilla icon