Many know it. Once again a migration was made and once again the problem with not transferred attributes occurs. In our case, it is a ShareGate migration from O365 to a local FileShare. But the file attributes “Created” and “Modified” are from the time of the migration itself and do not reflect the last editing status. But there is an easy solution to this problem! In the following post we show a simple code snippet that uses the CSV exported from ShareGate to set the original data on the local files. This way you can easily restore the Created and Modified attributes after the migration.

The CSV exported from ShareGate to restore the Created and Modified attributes after a migration

This file contains the necessary attributes. With PowerShell it is easy to iterate and reset them on the local files.

This is the PowerShell code snippet that sets the attributes on ShareGate files:

$csv = Import-Csv "E:\Export\ShareGateAttributes.csv" -Delimiter ';'
$RootFolder = "E:\ShareGate Export\"

#Process the Line that's currently in the pipeline
$csv | ForEach-Object {
    #Correct ShareGate URL folder format    
    $filePath = $RootFolder + $_.DestinationPath.Replace("/","\")

    #Modifiy local created/modified according to CSV
    try {
        $(Get-Item $filePath).creationtime = $(Get-Date $_.Created)
        $(Get-Item $filePath).lastwritetime = $(Get-Date $_.Modified)
    }
    catch {
        Write-Error $_ -BackgroundColor Red
    }
}

With the help of these few lines, in PowerShell all the magic happens.
Here the assignment of the O365 ShareGate export files of the “Created” and “Modified” data takes place.

About the functioning of the script:
An import reads the ShareGate CSV. After that PowerShell already recognizes the columns in the header and provides them in the object.
Using the ForEach-cmdlet you read line by line, fetch the corresponding local file and set the data.