{"id":9251,"date":"2012-06-01T00:01:00","date_gmt":"2012-06-01T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2012\/06\/01\/use-powershell-to-modify-file-access-time-stamps\/"},"modified":"2012-06-01T00:01:00","modified_gmt":"2012-06-01T00:01:00","slug":"use-powershell-to-modify-file-access-time-stamps","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-modify-file-access-time-stamps\/","title":{"rendered":"Use PowerShell to Modify File Access Time Stamps"},"content":{"rendered":"<p><b>Summary<\/b>: Learn how to use Windows PowerShell to modify file creation and modification, and to access time stamps.\n<img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\">&nbsp;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?\n&mdash;TK\n<img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\">&nbsp;Hello TK,\nMicrosoft Scripting Guy, Ed Wilson, is here. When I was writing all of the scripts for the Windows&nbsp;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.<\/p>\n<h2>Changing file attributes<\/h2>\n<p>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 <b>Get-Member<\/b> cmdlet as shown here.<\/p>\n<p style=\"padding-left: 30px\">PS C:&gt; Get-Item C:Changeda.ps1 | gm -Name *time<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; TypeName: System.IO.FileInfo<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MemberType Definition<\/p>\n<p style=\"padding-left: 30px\">&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;- &#8212;&#8212;&#8212;-<\/p>\n<p style=\"padding-left: 30px\">CreationTime&nbsp;&nbsp; Property&nbsp;&nbsp; System.DateTime CreationTime {get;set;}<\/p>\n<p style=\"padding-left: 30px\">LastAccessTime Property&nbsp;&nbsp; System.DateTime LastAccessTime {get;set;}<\/p>\n<p style=\"padding-left: 30px\">LastWriteTime&nbsp; Property&nbsp;&nbsp; System.DateTime LastWriteTime {get;set;}\nAs shown here, three properties end with the word <b>Time<\/b><i>. <\/i>In addition, all three properties appear as <b>get;set<\/b>, 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 <b>Get-Item<\/b> cmdlet to retrieve basic information about a text file named <b>a.txt<\/b>.<\/p>\n<p style=\"padding-left: 30px\">PS C:&gt; Get-Item C:fsoa.txt<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; &nbsp;Directory: C:fso<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">Mode&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; LastWriteTime&nbsp;&nbsp;&nbsp;&nbsp; Length Name<\/p>\n<p style=\"padding-left: 30px\">&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;&#8212;-&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212; &#8212;-<\/p>\n<p style=\"padding-left: 30px\">-a&#8212;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 6\/12\/2007&nbsp;&nbsp; 1:55 PM&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 3502 a.txt\nOne way to change the <b>LastWriteTime<\/b><i> <\/i>property is to store the <b>FileInfo<\/b> object in a variable, and then use the equals operator to assign a new value to the property. In the code that follows, the <b>Get-Item<\/b> cmdlet retireves the <b>FileInfo<\/b> object for the a.txt text file. Then I assign a new value to the <b>LastWriteTime<\/b> property. The new value is the current date and time retrieved via the <b>Get-Date<\/b> cmdlet. Finally, the basic properties of the file display.<\/p>\n<p style=\"padding-left: 30px\">PS C:&gt; $a = Get-Item C:fsoa.txt<\/p>\n<p style=\"padding-left: 30px\">PS C:&gt; $a.LastWriteTime = (get-date)<\/p>\n<p style=\"padding-left: 30px\">PS C:&gt; Get-Item c:fsoa.txt<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; Directory: C:fso<\/p>\n<p style=\"padding-left: 30px\">Mode &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;LastWriteTime&nbsp;&nbsp;&nbsp;&nbsp; Length Name<\/p>\n<p style=\"padding-left: 30px\">&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;&#8212;-&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212; &#8212;-<\/p>\n<p style=\"padding-left: 30px\">-a&#8212;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 5\/31\/2012&nbsp; 10:14 AM&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 3502 a.txt<\/p>\n<h2>Creating a function<\/h2>\n<p>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 <b>Path<\/b> parameter is a mandatory parameter. This portion of the function is shown here:<\/p>\n<p style=\"padding-left: 30px\">Param (<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; [Parameter(mandatory=$true)]<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; [string[]]$path,<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; [datetime]$date = (Get-Date))\nThe main portion of the function uses the <b>Get-ChildItem<\/b> cmdlet to retrieve all files and folders in the current path. It does not use the <i>Recurse <\/i>switched parameter, but if you want to add it, you could. For my purposes, I do not want it to <i>Recurse, <\/i>so I left the switch off. Next, the objects pass to the <b>Foreach-Object<\/b> 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.<\/p>\n<p style=\"padding-left: 30px\">Set-FileTimeStamps -path C:Ref -date 7\/1\/11\nThe complete <strong>Set-FileTimeStamps<\/strong> function is shown here:<\/p>\n<p style=\"padding-left: 30px\"><b>Set-FileTimeStamps function<\/b><\/p>\n<p style=\"padding-left: 30px\">Function Set-FileTimeStamps<\/p>\n<p style=\"padding-left: 30px\">{<\/p>\n<p style=\"padding-left: 30px\">&nbsp;Param (<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; [Parameter(mandatory=$true)]<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; [string[]]$path,<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; [datetime]$date = (Get-Date))<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; Get-ChildItem -Path $path |<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; ForEach-Object {<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp; $_.CreationTime = $date<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp; $_.LastAccessTime = $date<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp; $_.LastWriteTime = $date }<\/p>\n<p style=\"padding-left: 30px\">} #end function Set-FileTimeStamps\nTK, that is all there is to using Windows PowerShell to modify time stamps on files.&nbsp; 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.\nI invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.\n<b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Learn how to use Windows PowerShell to modify file creation and modification, and to access time stamps. &nbsp;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 [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[38,3,4,45],"class_list":["post-9251","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-files","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Learn how to use Windows PowerShell to modify file creation and modification, and to access time stamps. &nbsp;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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/9251","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=9251"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/9251\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=9251"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=9251"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=9251"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}