how to use powershell to copy file and retain original timestamp

18,026

Solution 1

Here's a powershell function that'll do what you're asking... It does absolutely no sanity checking, so caveat emptor...

function Copy-FileWithTimestamp {
[cmdletbinding()]
param(
    [Parameter(Mandatory=$true,Position=0)][string]$Path,
    [Parameter(Mandatory=$true,Position=1)][string]$Destination
)

    $origLastWriteTime = ( Get-ChildItem $Path ).LastWriteTime
    Copy-Item -Path $Path -Destination $Destination
    (Get-ChildItem $Destination).LastWriteTime = $origLastWriteTime
}

Once you've run loaded that, you can do something like:

Copy-FileWithTimestamp foo bar

(you can also name it something shorter, but with tab completion, not that big of a deal...)

Solution 2

Here is how you can copy over the time stamps, attributes, and permissions.

$srcpath = 'C:\somepath'
$dstpath = 'C:\anotherpath'
$files = gci $srcpath

foreach ($srcfile in $files) {
  # Build destination file path
  $dstfile = [io.FileInfo]($dstpath, '\', $srcfile.name -join '')

  # Copy the file
  cp $srcfile.FullName $dstfile.FullName

  # Make sure file was copied and exists before copying over properties/attributes
  if ($dstfile.Exists) {
    $dstfile.CreationTime = $srcfile.CreationTime
    $dstfile.LastAccessTime = $srcfile.LastAccessTime
    $dstfile.LastWriteTime = $srcfile.LastWriteTime
    $dstfile.Attributes = $srcfile.Attributes
    $dstfile.SetAccessControl($srcfile.GetAccessControl())
  }
}

Solution 3

If you are okay with a two-step solution; then

  • First copy files over from source to dest
  • loop over each file; and each attribute for each file
  • copy attributes from source to destination

Try this technique to copy file attributes from one file to another. (I have illustrated this with LastWriteTime; I am sure you can extend it for other attributes).

#Created two dummy files
PS> echo hi > foo
PS> echo there > bar

# Get attributes for first file
PS> $timestamp = gci "foo"
PS> $timestamp.LastWriteTime

06 February 2014 09:25:47

# Get attributes for second file
PS> $t2 = gci "bar"
PS> $t2.LastWriteTime

06 February 2014 09:25:53

# Simply overwrite
PS> $t2.LastWriteTime = $timestamp.LastWriteTime

# Ta-Da!
PS> $t2.LastWriteTime

06 February 2014 09:25:47
Share:
18,026
Admin
Author by

Admin

Updated on June 17, 2022

Comments

  • Admin
    Admin almost 2 years

    I would like to copy some files or a folder of files from one file server to another. However, I want to keep the original timestamp and file attributes so that the newly copied files will have the same timestamp of the original files. Thanks in advance on any answer.

  • Guy Thomas
    Guy Thomas over 10 years
    Neat. It worked when I tested Copy-FileWithTimestamp.
  • Hunter Eidson
    Hunter Eidson over 10 years
    I haven't tested it, but I would expect it to fail spectacularly if you tried to use a wildcard in the copy the way I wrote that function...
  • Guy Thomas
    Guy Thomas over 10 years
    I merely wanted to help the community by reporting that I tried your code and it worked. I did not mean to imply that I was giving an endorsement for use in a production network.
  • Hunter Eidson
    Hunter Eidson over 10 years
    No problem... my comment was more of an afterthought when I was driving into the office today... :)
  • jpaugh
    jpaugh over 7 years
    Using Get-Item would probably be safer, especially if $Destination is a directory.
  • stackprotector
    stackprotector over 2 years
    Nice reminder to take into account all three timestamps and not just LastWriteTime.