Wednesday, January 28, 2015

#42 : How to change modified date of file using Powershell?

I always had a technique in Unix which converts modified date of a file. But never tried the same in Windows. But it was again possible in Windows using MKS Toolkit or Cygwin. There are very rare situation where you need to modified date of file. I don't want to go depth of reason, let's see
how we can do it.

DESCRIPTION

Run the below command:
ls

Directory: C:\Users\admin\Downloads


Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 9/22/2012 9:37 PM 4750740 Setup_BullzipPDFPrinter_9_0_0_1437.zip
-a--- 9/23/2012 10:10 PM 58158 webcam-toy-photo1.jpg
-a--- 9/23/2012 10:10 PM 58158 webcam-toy-photo2.jpg



Ok, you may notice the LastWriteTime column. This column can be simply used to change the date and time.

Let's rename webcam-toy-photo2.jpg with modified date as 9/23/1942 10:10 PM.

Run the below command:

ls | where { $_.Name -eq "webcam-toy-photo2.jpg" } | foreach { $_.LastWriteTime="9/23/1942 10:10 PM" }

Let's check the date again. The date is of 1942.

ls

Directory: C:\Users\admin\Downloads


Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 9/22/2012 9:37 PM 4750740 Setup_BullzipPDFPrinter_9_0_0_1437.zip
-a--- 9/23/2012 10:10 PM 58158 webcam-toy-photo1.jpg
-a--- 9/23/1942 10:10 PM 58158 webcam-toy-photo2.jpg



Enjoy Scripting!!

3 comments:

  1. Well done!! Works perfectly if you have only 1 file to change.

    ReplyDelete
  2. By the way, I used PowerShell in Windows 10 ...

    ReplyDelete
  3. Your website is very beautiful or Articles. I love it thank you for sharing for everyone. Curso oficial Linux LPIC-2 - Exam 202 + examen

    ReplyDelete

#112: How to handle xml document in Powershell?

 In PowerShell, you can handle XML data using various cmdlets and methods provided by the .NET Framework. Here's a basic guide on how to...