Copy-item copies the folder and changes timestamp (Doesn't preserve timestamp)

804 Views Asked by At

I'm currently creating Powershell script to copy the folder to the remote servers I use copy-item cmdlet which changes the timestamp for the folders.

is there a way to copy folder preserving timestamp of folders?

Thank you in advance

2

There are 2 best solutions below

0
On

When you copy directory, you just create new direcory with same name in diffrent location

param(
    [System.IO.DirectoryInfo]$sourecDir = "D:\tmp\001",
    [System.IO.DirectoryInfo]$destDir = "D:\tmp\002"
)

[System.IO.Directory]::CreateDirectory("D:\tmp\002")
$destDir.CreationTime = $sourecDir.CreationTime
0
On

I'm looking for answer of the same question, but it seems that Copy-Item didn't have this feature.

I'm using Robocopy currently, which is a built-in command of Windows. You can use /dcopy:DAT to copy data, attributes and timestamp of folders/directories.