Forum Discussion
Powershell-Read multiple csv files available in SharePoint online document library and Merge all
How can i save my output csv (merged file)in sharepoint library folder? how can i remove the duplicate header
Hi KrisSub2023,
To save the merged CSV file directly to a SharePoint Online document library folder, you can try to use this modified script:
# Connect to SharePoint Online
Connect-PnPOnline -Url "https://your-sharepoint-site-url" -UseWebLogin
# Set the document library and target folder path
$documentLibraryName = "YourDocumentLibraryName"
$targetFolderPath = "/sites/YourSite/YourDocumentLibraryName/FolderName" # Update with the desired folder path
# Get all CSV files from the document library
$csvFiles = Get-PnPListItem -List $documentLibraryName | Where-Object { $_.File.Name -like "*.csv" }
# Initialize an empty array to store content from all CSV files
$mergedContent = @()
# Read content from each CSV file and store it in the $mergedContent array
foreach ($file in $csvFiles) {
$fileContent = Get-PnPFile -Url $file.FieldValues.FileRef -AsString
$mergedContent += $fileContent
}
# Create the merged CSV file in the target folder
$mergedFilePath = Join-Path $targetFolderPath "MergedFile.csv"
$mergedContent | Out-File -FilePath $mergedFilePath -Encoding UTF8
# Upload the merged CSV file to SharePoint document library folder
Add-PnPFile -Path $mergedFilePath -Folder $targetFolderPath -List $documentLibraryName
# Disconnect from SharePoint Online
Disconnect-PnPOnline
Write-Host "All CSV files have been merged and saved to $mergedFilePath in the SharePoint document library folder."
In this modified script, you need to update the `$targetFolderPath` variable with the desired folder path in the SharePoint document library where you want to save the merged CSV file. The `$targetFolderPath` variable should be set to a relative URL starting with `/sites/` followed by your site name, document library name, and the folder name where you want to save the file.
Please click Mark as Best Response & Like if my post helped you to solve your issue.
This will help others to find the correct solution easily. It also closes the item.
If the post was useful in other ways, please consider giving it Like.
Kindest regards,
Leon Pavesic