none
Powershell change from current date and time to 1990 on multiple files RRS feed

  • General discussion

  • Hi,

    How to make a script using the FileInfo to change LastWriteTime in the directory specified in the script, so that the first file was created LastWriteTime 01/01/1990 00:00 and next one year later. Can someone create a script and give a description of the functions.

    Wednesday, December 9, 2015 1:51 PM

All replies

  • where is your current script and what problem are you having? 

    some cmdlets you may want to use include get-childitem, set-itemproperty, and get-date.

    I would start by setting a variable such as $timestamp with a base date of 01/01/1990 and checking out the AddYears method on Get-Date

    Wednesday, December 9, 2015 2:52 PM
  • Hey Nick,

    My friend is teaching me PowerShell. This is my 2 day and he already put me in deap water. So far, I have

    $a = Get-ChildItem "c:\temp\"

    {

    (Get-itemproperty $*.txt) |

    set-itemproperty Foreach-Object {$_ -replace "<CreationTime>.*<CreationTime >", "<LastWriteTime>01/01/1990</LastWriteTime>"} |

    Set-itemproperty $file.txt}

    but i have this error msg

    Get-ChildItem : Illegal characters in path.
    At line:1 char:6
    + $a = Get-ChildItem "c:\temp\"{(Get-itemproperty $*.txt) | set-itemproperty Forea ...
        + CategoryInfo          : InvalidArgument: (C:\temp\:String) [Get-ChildItem], ArgumentExcept
        + FullyQualifiedErrorId : DirArgumentError,Microsoft.PowerShell.Commands.GetChildItemCommand

    Wednesday, December 9, 2015 3:31 PM
  • As you can see, it's not going to be possible to guess your way to a solution.

    You need to start by learning PowerShell. There are lots of good resources available. For example:

    http://blogs.technet.com/b/heyscriptingguy/archive/2015/01/04/weekend-scripter-the-best-ways-to-learn-powershell.aspx


    -- Bill Stewart [Bill_Stewart]

    Wednesday, December 9, 2015 3:36 PM
    Moderator
  • Yep, this is definitely way too complicated for your 2nd day!

    Bill's post above is a great consolidated list of resources, so if your friend doesn't have any books or training materials (or even if they do!) definitely check out those links. 

    As far as your script, the first line is good, but the rest needs some work. Also always ensure you're working in the right path, as you can cause some major problems adjusting these values in, for example, c:\windows\system32

    The next variable you create should be your base date: 01/01/1990 00:00

    $timestamp = Get-Date "01/01/1990 00:00"

    Now once you have that $a list of files, you can send it to a foreach block.  this is how you'll do whatever you want to do to each one of those files:

    $a | ForEach-Object {}

    Inside the script block (the {} after foreach) will be the code that makes the changes.  You've got the right idea with the property names - you'll want to change each of: CreationTime, LastWriteTime, and possibly LastAccessTime

    you can do this by simply applying the $timestamp as a value for each of those properties like so:

    $a | ForEach-Object {

    Set-ItemProperty $_ -Name CreationTime -Value $timestamp

    Set-ItemProperty $_ -Name LastWriteTime -Value $timestamp

    }

    now last you want to incrementally add a year for each object in the list.  To accomplish this you would make a few changes: before the foreach block, you'll want to set the counter to 0, so no matter how many times this runs, you'll always start with a count of 0. 

    $counter = 0

    Inside the foreach block, the counter variable will need a way to incrementally add a count on each pass, and a new timestamp variable will be created which will temporarily hold the value of the timestamp plus the counter value for each item processed.

    So each item that's processed will add a value of 1 to the $counter variable.  This value will get added to the $timestamp variable using the AddYears method.

    $counter += 1

    $newtimestamp = $timestamp.AddYears($count)

    lastly, adjust the set-itemproperty lines to use the $newtimestamp variable instead of $timestamp and you'll get something like this:

    $a = ls
    $timestamp = get-date "01/01/1990 00:00"
    $counter = 0
    
    $a | ForEach-Object {$counter += 1
        $newtimestamp = $timestamp.AddYears($counter)
        Set-ItemProperty $_ -Name CreationTime -Value $newtimestamp
        Set-ItemProperty $_ -Name LastWriteTime -Value $newtimestamp
        }


    • Edited by -Nick Thursday, December 10, 2015 4:18 PM
    Thursday, December 10, 2015 4:18 PM
  • Bill, thank you for this link with information. I will definitely use this in my studies on PS. Sometimes it is hard to change the way of thinking about new logic and point of view, form the program site. As you saw, I tried to make a small car out of a bus and I know that there is a long way before me to make it :). Again THX
    Thursday, December 10, 2015 5:14 PM
  • Hey Nick,

    WOW!!!!  Thank you for providing the detailed information of the path and about the path. Also, thx for making me causing about which path I use. For no experience use of PS, I can do a lot of damage on and to my OS. Adding to this, for first time, this is a lot of information to coup with. You can read and find description of each commend, how to apply it and see some examples but when you need to use more than one or use different commends, there is a need to know which first, second to receive the result is. For right now, it is mind blowing and cause headache but this looks great.

    Yesterday and today, i was reading and looking more, to how to make my script to work. i have found, that i can use DateTime and FileInfo on LastWriteTime how to manipulate with time on the files. So, I came to this

    $a = ls
    foreach($file in $DateTime)
    {
    DateTime = new DateTime(1990, 1, 1, 0, 0, 0);
    set-ChildItem  -Recurse | Where-Object { $_.LastWriteTime }
    }

    Now there are no errors but the dates are not changing.

    Also looking here, I'm noticing that there is many ways of getting to the same conclusion, so i can compare it to may recipes how to make pie.

    Thursday, December 10, 2015 7:09 PM
  • Now, query works but mu counter is not working like, I would like to. I'm trying to focus on .net in PS.

    $a = ls
    $counter = 1990
    foreach($file in $a)
    {
        $datetime = new-object DateTime($counter, 01, 01, 00, 00, 00)
        get-childitem | foreach-object { $_.LastWriteTime = $datetime }
        $counter ++
    }

    Can someone help, and look in this script

    Thursday, December 17, 2015 4:41 PM
  • Now, query works but mu counter is not working like, I would like to. I'm trying to focus on .net in PS.

    $a = ls
    $counter = 1990
    foreach($file in $a)
    {
        $datetime = new-object DateTime($counter, 01, 01, 00, 00, 00)
        get-childitem | foreach-object { $_.LastWriteTime = $datetime }
        $counter ++
    }

    Can someone help, and look in this script.

    Thursday, December 17, 2015 4:42 PM
  • So you want to set all files in the current location to have a LastWriteTime of 1/1/1990.

    Is that correct? It would look like this:


    Get-ChildItem -File | ForEach-Object { $_.LastWriteTime = [DateTime] "1/1/1990" }
    

    What is the purpose?


    -- Bill Stewart [Bill_Stewart]


    Thursday, December 17, 2015 5:22 PM
    Moderator
  • No, like it is in description in the beginning of the blog " change the date on first file on 01/01/1990 00:00 and next one year later in provided in script".

    Now my script is changing date of the files but all of the have the same not one year late. I used twice commend counter in the script on what to change but it is not counting.

    This is for learning purposes and I'm having counting issue.

    Thursday, December 17, 2015 6:44 PM
  • You mean this?


    $assignDate = [DateTime] "1/1/1990"
    Get-ChildItem -File | ForEach-Object {
      $_.LastWriteTime = $assignDate
      $assignDate = $assignDate.AddYears(1)
    }
    


    -- Bill Stewart [Bill_Stewart]

    Thursday, December 17, 2015 8:35 PM
    Moderator
  • I was finally do this:

    $a = ls
    $counter = 1990
    foreach($file in $a)
    {
        $datetime = new-object DateTime ($counter, 01, 01, 00, 00, 00)
        $file.LastWriteTime = $datetime
        $counter++
    }

    Thursday, December 24, 2015 4:14 PM