Merge Multiple CSV Files With Same Header Using Windows PowerShell

Currently, I am working on eRoom to SharePoint migration project with the help of Metalogix. Metalogix eRoom console has a feature to run pre-migration check to identify possible migration issues which could occur during migration that includes the following:

  • Document Url length
  • Document Size
  • Document Name
  • Unsupported file types

Identifying these possible issues is necessary so that you can plan for these failures by talking to end user. Metalogix eRoom console generate report of each of these issues based on source eRoom and target SharePoint site where the content will be migrated. These reports are generated in the form of .csv files. Every eRoom pre-migration check feature generates 4 such files. For us, we have to migrate approximately 230 eRoom to SharePoint 2010 environment. So we wanted to run pre-migration check to all of these 230 eRooms and generate consolidated report so that we can review it with business user.

Now to combine these 230 csv files for every issue could take days and involve lot of manual efforts. To avoid those manual efforts, I decided to use Windows PowerShell which provides tons of cmdlets (command lets) which could do easy trick to avoid such manual efforts.

I have created PowerShell function with the following:

Accepts 3 parameters

  • $Path – Path of the directory where all the csv files are stored.

  • $MergedFileName – Name of the file which would be created by merging target multiple csv. files.

  • $Filter – Any filter criteria which could be used to filter the list of target csv files to be merged.

Based on the path and filter criteria, it finds list of files to be merged.

  1. if($Filter -ne $null -and $Filter -ne "")  
  2. {  
  3.    $AllFilesToMerge = Get-ChildItem -Path $Path -File -Filter $Filter -Recurse  
  4. }  
  5. else  
  6. {  
  7.    $AllFilesToMerge = Get-ChildItem -Path $Path -Recurse  
  8. }  
Then it process each file by importing it.
  1. $csvFilePath = $FileToMerge.FullName  
  2. $csvContent = Import-Csv -Path "$csvFilePath"  
Then it reads the file content based on the headers of the csv file. Every row in the csv is stored in temporary PSObject variable which is then added to another global PSObject.

Note: You need to update the script for your csv headers.
  1. if ($csvContent - ne $null) {  
  2.     foreach($entry in $csvContent) {#  
  3.         You can also get the headers of particular.csv file like below - commented# $CSVContentObj = $csvContent | Get - Member - MemberType NoteProperty | Select - Object - ExpandProperty Name# Creating custom PS Object to hold the content temporarily  
  4.         $CSVContentObj = 1 | select RoomName, SourceName, SourceURL, RectifiedName  
  5.         $CSVContentObj.RoomName = $roomName# TODO: User need to update the headers manually before using this  
  6.         function# $entry.  
  7.         "<HeaderName>"  
  8.         $CSVContentObj.SourceName = $entry.  
  9.         "Source Name"  
  10.         $CSVContentObj.SourceURL = $entry.  
  11.         "Source URL"  
  12.         $CSVContentObj.RectifiedName = $entry.  
  13.         "Rectified Name"#  
  14.         Adding custom PS object in the arrary  
  15.         $CSVContentList += $CSVContentObj  
  16.     }  
  17. }   
  18. else {  
  19.     Write - Host - ForegroundColor Yellow "File don't have any content"  
  20. }  
Finally, if the global PSObject which holds all the content is not null, export the content in target csv file.
  1. if ($CSVContentList - ne $null) {#  
  2.     Getting current script execution location  
  3.     $CurrentLoc = Get - Location  
  4.     if ($MergedFileName.IndexOf(".csv") > 0) {  
  5.         $csvPath = "$CurrentLoc\$MergedFileName"  
  6.     }   
  7.     else {  
  8.         $csvPath = "$CurrentLoc\$MergedFileName.csv"  
  9.     }#  
  10.     Exporting the content as csv file  
  11.     $CSVContentList | Export - Csv - Path $csvPath  
  12.     Write - Host - ForegroundColor Green "Files merged successfully and exported at "  
  13.     $csvPath  
  14. }  
The complete PowerShell script is attached to this article.

Up Next
    Ebook Download
    View all
    Learn
    View all