Copy Files and attributes with Powershell.

Hi, I have a script that saves all files that need to be copied to another directory into a CSV file. The CSV file has a list of all filenames in it, and I feed the names as a variable into Copy-Item - 

$output | select-object FileName | Export-Csv -Path C:\Changes.csv -NoTypeInformation $NewVersions = import-csv -Path "C:\changes.csv" forEach ($row in $NewVersions) { $filename = $row.FileName Copy-Item "C:\SOURCE\$filename" "C:\DESTINATION" write-host "Copying - " $filename }


I've noticed, the script doesn't copy the file attributes along with it, for example, date created, date modified etc. Is there any way that I can modify the script so that it copies the file and its attributes?

Thanks. 


August 27th, 2015 12:39pm

This should do your task, probably you have to add more properties, if wanted:

$output | select-object FileName | Export-Csv -Path C:\Changes.csv -NoTypeInformation
$SourceDir = "C:\Source"

$NewVersions = Import-Csv -Path "C:\changes.csv" 
foreach($row in $NewVersions)
{
    $filename = $row.FileName
    $OldFile = Get-ChildItem "$SourceDir\$filename"
    $NewFile = Copy-Item $OldFile.Fullname "C:\DESTINATION" -PassThru

    "CreationTime","LastWriteTime","LastAccessTime" | foreach{
        $NewFile.$_ = $OldFile.$_
    }

    write-host "Copying - " $filename
}
However, I don't really understand, why you do this laborious csv-Export and Import.

Free Windows Admin Tool Kit Click here and download it now
August 27th, 2015 2:05pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics