Update "Windows Explores modyfied time stamp" using copy-item Cmdlet

This topic contains 4 replies, has 4 voices, and was last updated by Profile photo of Tech Gismo Tech Gismo 1 year, 8 months ago.

  • Author
    Posts
  • #25378
    Profile photo of Tech Gismo
    Tech Gismo
    Participant

    I do a lot of copying files around from test to produktion enviroments and for this I have chosen to use the Copy-item Cmdlet. It works for me.

    One thing that I have searched an answer for is this: How can I force the timestamp to be updated when a file is overwritten.

    Let's say I have a file called "File A" with a date 01-05-2015.
    in this path \\serverB\temp I also have a file called File A but with an earlier timestamp 01-01-2015.

    Now I copy File A – the new one and I overwrite the older file also named file A. But the file keeps the old time stamp 01-01-2015. How can I force it to be updated to 01-05-2015 from 01-01-2015.

    Thanks a lot for the help in advance.

    copy-item \\Server-A\FolderName\FileNameA -Recurse -Destination \\Server-B\FolderNameWithOldFileA\ -Force -Verbose
  • #25381
    Profile photo of GJ
    GJ
    Participant

    I am not able to replicate your issue unless I misunderstood your question. It always copies/overwrites the file with all it's properties.

    
    PS C:\> dir '\\server1\c$\servers.txt'
    
    
        Directory: \\server1\c$
    
    
    Mode                LastWriteTime     Length Name
    ----                -------------     ------ ----
    -a---          1/9/2015   4:24 AM         23 servers.txt
    
    
    PS C:\> Copy-Item '\\server1\c$\servers.txt' '\\server2\c$\' -Verbose -Force
    VERBOSE: Performing the operation "Copy File" on target "Item: \\server1\c$\servers.txt Destination:
    \\server2\c$\servers.txt".
    PS C:\> dir '\\server2\c$\servers.txt'
    
    
        Directory: \\server2\c$
    
    
    Mode                LastWriteTime     Length Name
    ----                -------------     ------ ----
    -a---          1/9/2015   4:24 AM         23 servers.txt
    
    
    PS C:\> echo something >>'\\server1\c$\servers.txt'
    PS C:\> dir '\\server1\c$\servers.txt'
    
    
        Directory: \\server1\c$
    
    
    Mode                LastWriteTime     Length Name
    ----                -------------     ------ ----
    -a---         5/19/2015   4:52 PM         45 servers.txt
    
    
    PS C:\> Copy-Item '\\server1\c$\servers.txt' '\\server2\c$\' -Verbose -Force
    VERBOSE: Performing the operation "Copy File" on target "Item: \\server1\c$\servers.txt Destination:
    \\server2\c$\servers.txt".
    PS C:\> dir '\\server2\c$\servers.txt'
    
    
        Directory: \\server2\c$
    
    
    Mode                LastWriteTime     Length Name
    ----                -------------     ------ ----
    -a---         5/19/2015   4:52 PM         45 servers.txt
    
    
    PS C:\>
    
    
    
    
  • #25382
    Profile photo of nohandle
    nohandle
    Member

    The LastWriteTime and CreationTime properties of the FileInfo object are writable. So you can simply use the -PassThru parameter to output the destination file object and assing the current time:

    Copy-Item C:\temp\p.txt C:\Temp\p_copy.txt -PassThru | foreach {
        $_.lastWriteTime = $_.CreationTime = Get-Date
    }
    
  • #25383
    Profile photo of Tim Pringle
    Tim Pringle
    Participant

    Hey Tech Gismo,

    The Creationtime, LastAccessTime, and LastWritetime attributes of a file are able to be set and well as the information retrieved from. All you should need to do is get the file and then update the property.

    What timestamp are you referring to though? LastWriteTime attribute should be updated automatically to that of the source file when using copy-item, so is it one of the other two?

    UPDATE: LOL, beaten to it!

  • #25386
    Profile photo of Tech Gismo
    Tech Gismo
    Participant

    Thanks this works great, but what if I have 10 rows, like this:

    copy-item \\Server-A\FolderName\FileNameA -Recurse -Destination \\Server-B\FolderNameWithOldFileA\ -Force -Verbose
    copy-item \\Server-A\FolderName\FileNameA -Recurse -Destination \\Server-B\FolderNameWithOldFileB\ -Force -Verbose
    copy-item \\Server-A\FolderName\FileNameA -Recurse -Destination \\Server-B\FolderNameWithOldFileC\ -Force -Verbose
    copy-item \\Server-A\FolderName\FileNameA -Recurse -Destination \\Server-B\FolderNameWithOldFileD\ -Force -Verbose
    copy-item \\Server-A\FolderName\FileNameA -Recurse -Destination \\Server-B\FolderNameWithOldFileE\ -Force -Verbose

    Is there a smarter method then?

You must be logged in to reply to this topic.