Tuesday, October 29, 2019

Compare files with SharePoint library after file server to SharePoint migration

#This script compares the folder contents with SharePoint site. This can be used to find out the files that are not migrated to SharePoint from File server
# Input 1 variable $siteURL : Provide the SharePoint Site URL; Format: "https://sharepoint.domain.com/HEM"
# Input 2 variable $filePath : Shared Drive path where the contents need to be compared with; Format: K:\HES\MAIN RECORDS
# Input 3 variable $docLibURL : SharePoint Document library URL where the contents to be compared with; Format: https://sharepoint.domain.com/HEM/Docs
# Input 4 variable $siteName: Used to suffix the export file name

Add-PSSnapin Microsoft.SharePoint.PowerShell -EA silentlycontinue
$siteURL = "https://sharepoint.domain.com/HEM"
$filePath = "K:\HES\MAIN RECORDS"
$docLibURL = "https://sharepoint.domain.com/HEM/DOCS"
$siteName = "HEM_DOCS"

#Get all files from shared drive
$sFiles = get-childitem $filePath -Recurse | where {!$_.PSIsContainer} | select-object Name, FullName, LastWriteTime, Length

#Get SharePoint site object
$myweb =  Get-SPSite $siteURL

#Create a CSV file to export compare results
Add-Content -Path .\CompareResults_$siteName.csv  -Value 'FileName,Source,Destination'

Write-Host Comparing files, please wait...

$TotalFiles = $sFiles.Count;
$counter = 0 

#Get all files under the path specified
ForEach ($file in  $sFiles)
{

    #Prepare the file path for SharePoint based on the source file
    $spFilePath = $fileFullName.replace($filePath, $docLibURL ).replace('\','/')


    #Get file from SharePoint
    $f = $myweb.RootWeb.GetFile($spFilePath);

    #Check if file exists
    If (!($f.Exists)){
        $counter++
            #Export the file information to excel
            $Data = [PSCustomObject]@{
            'FileName' = $file.Name
            'Source'   = $file.FullName
            'Destination' = $spFilePath}
            $Data | Export-Csv -Path .\CompareResults_$siteName.csv -NoTypeInformation -append
        }
}

Write-Host Total files are $TotalFiles and missing Files $counter