Upload all the files in a folder to SharePoint online and preserve creation date using PNP module
Upload all the files in a folder to SharePoint online and preserver creation date using PNP module
Uploading all the files in a folder to SharePoint Online and preserving the original created date can be done using the PnP PowerShell module. Here is a step-by-step guide on how to do this:
- Install the PnP PowerShell module on your computer by running the following command in PowerShell: Install-Module SharePointPnPPowerShellOnline
- Register PnP management shell using the following command “Register-PnPManagementShellAccess”
- Use this script to upload all files in a folder and preserve the creation date
# Connect to SharePoint Online “Replace below Url with your site address”
Connect-PnPOnline-Urlhttps://m365test.sharepoint.com/sites/PreserveCreationDate-Credentials (Get-Credential)
# Set the library name “Replace below library name with your library name”
$libraryName="/sites/PreserveCreationDate/Shared Documents/Test Site"
# Set the folder path “Replace below folder path with your required path”
$folderPath="C:\Users\awais.khalid\Desktop"
foreach ($filein (Get-ChildItem$folderPath-File)) {
$filePath=$file.FullName
# Get the original created date of the file
$originalCreatedDate= (Get-Item$filePath).CreationTime
# Upload the file to the library and set the created date
Add-PnPFile-Path$filePath-Folder$libraryName-Values @{'Created'=$originalCreatedDate}
}
- This script uses the foreach loop to iterate over all the files in the specified folder path using the Get-ChildItem cmdlet, with the -File parameter to only get files. Then for each file, the script proceeds to upload the file to the SharePoint library and preserving the original created date of each file by using the Add-PnPFile cmdlet.
- Please make sure to replace the following placeholders in the script with your own values:
- https://yourtenant.sharepoint.com/sites/yoursite with the URL of your SharePoint site.
- yourlibraryname with the name of the library where you want to upload the document.
- C:\path\to\your\folder with the path to the folder which contains the files you want to upload.
- Once the foreach loop completes, the script should have uploaded all the files in the folder to SharePoint Online and preserved the original created date for each file.
Last updated on