SPDocVersion-Exporter.ps1
<#PSScriptInfo .VERSION 1.0 .GUID f47abff3-8d54-4a9d-bac5-a558ae0fb43a .AUTHOR Mohamed El-Qassas .COMPANYNAME debug.to .COPYRIGHT All copy reserved to https://debug.to .TAGS SharePoint,Document Library,export .LICENSEURI https://spgeeks.devoworx.com .PROJECTURI https://spgeeks.devoworx.com/export-sharepoint-version-history-to-excel/ .ICONURI .EXTERNALMODULEDEPENDENCIES Microsoft.SharePoint.PowerShell .REQUIREDSCRIPTS .EXTERNALSCRIPTDEPENDENCIES .RELEASENOTES .PRIVATEDATA #> <# .DESCRIPTION This script "SPDocVersion-Exporter" would help you to export SharePoint Version History with comments to CSV/Excel Using PowerShell For more details about the script features and how this script works, Please check https://spgeeks.devoworx.com/export-sharepoint-version-history-to-excel/ #> Param() # Author: Mohamed El-Qassas # Blog : https://devoworx.com # Date : 04/01/2021 # Read instrcutions at https://spgeeks.devoworx.com/export-sharepoint-version-history-to-excel/ # Description: PowerShell Script to Get the Version Comments for each Document and other Version Details in SharePoint Document Library # Have a quiestion, Please ask it at https://debug.to #Add Add-PSSnapin Microsoft.SharePoint.PowerShell Set-ExecutionPolicy "Unrestricted" Add-PSSnapin "Microsoft.SharePoint.PowerShell" #Variables function Export-DocLibVersions () { param([Parameter(Mandatory=$true)][string]$siteURL,[Parameter(Mandatory=$true)][string]$DocLibName) try { #################################################################### #Get the Web and document library $WebSite = Get-SPWeb $siteURL #$DocLib = $WebSite.Lists.TryGetList($DocLibName) #################################################################### #check the website URL Write-Host "#####################################`n SharePoint Doc Library Versions Exporter`n#####################################" -ForegroundColor Green if((Get-SPSite $siteURL -ErrorAction SilentlyContinue) -eq $null) { Write-Host "The provided SharePoint Site URL $siteURL is not found,`n Please, provide a valid SharePoint URL!" -ForegroundColor Red Export-DocLibVersions } #Check if the doc lib is found $DocLib = $WebSite.Lists.TryGetList($DocLibName) if($DocLib -eq $null) { Write-host "The document library ($DocLibName) is not found! please prvoide a correct Document Library Name located at $siteURL" -ForegroundColor Red Export-DocLibVersions } Write-host "Start Exporting process ......." -ForegroundColor cyan #Check if the version settings is enabled to proceed If ($DocLib.EnableVersioning -eq $TRUE) { Write-host ">> The version settings is enabled for $DocLibName" -ForegroundColor Cyan #Prepare the file Path $ReportFolderPath = "C:\SPVersions\$DocLibName\LibVersions-$((Get-Date).ToString('yyyy-MM-dd-hh-mm-ss-tt'))" $ReportFilePath = "$ReportFolderPath\versions.csv" #check the folder path if(!(Test-path $ReportFolderPath)) { New-Item -ItemType Directory -Path $ReportFolderPath } #check the Report path if(!(Test-path $ReportFilePath -PathType leaf)) { New-Item -ItemType file -Path $ReportFilePath } #Get all documents $DocsCollection = $DocLib.Items if($DocsCollection -ne $null) { #iterate for each doc in document library foreach ($Doc in $DocsCollection) { $DocName = $Doc.Name Write-host "Exporting versions for ($DocName) ......." -ForegroundColor green $Vcount = ($Doc.Versions).Count #Get the Version Count for the current document Write-host ">> Number of versions for ($DocName) is ($Vcount)" -ForegroundColor cyan $totalVersions=$Vcount #Total Versions for all documents $count= $Vcount #Set the decremental count to the Version Count #Iterate for each version in each document $docURL = $siteURL+$Doc.URL # get full document URL Add-Content -Path $ReportFilePath -Value "Doc ID,Document Name,Doc URL, Total Versions, Created By, Created Date, Modified By, Modified Date ,Size (KB)" Add-Content -Path $ReportFilePath -Value "$($Doc.id),$($DocName),$($docURL),$($Vcount),$($Doc['Author']),$($Doc['Created']),$($Doc['Editor']),$($Doc['Modified']),$($Doc.File.Length)" Add-Content -Path $ReportFilePath -Value "Version ID,Version Title, Modified By, Modified at ,Size (KB), CheckInComments" foreach($version in $Doc.Versions) { if([int]$count -eq $Vcount) { $VersionDetails = "$($version.VersionLabel),$($version['Title']), $($version.CreatedBy.User.DisplayName), $($version.Created),$($Doc.File.Versions.GetVersionFromLabel($version.VersionLabel).Size),$($version['Check In Comment'])" } else { $VersionDetails = "$($version.VersionLabel),$($version['Title']), $($version.CreatedBy.User.DisplayName), $($version.Created),$($Doc.File.Versions.GetVersionFromLabel($version.VersionLabel).Size),$($Doc.File.Versions.GetVersionFromLabel($version.VersionLabel).CheckInComment)" } # add the version details to the exported file Add-Content -Path $ReportFilePath -Value $VersionDetails $count = $count -1 #decrease version count } #Add Item Sperator Add-Content -Path $ReportFilePath -Value "---------------------------------------------------" $totalVersion = $totalVersion + $Vcount } Write-host "The versions of $DocLibName have been exported successfuly at $ReportFilePath" -ForegroundColor Green Start-Process $ReportFilePath $DocCount = $DocsCollection.Count Write-host "------------------------------------------------------" -ForegroundColor Cyan Write-host "Export Operation Summary" Write-host "------------------------------------------------------" -ForegroundColor Cyan Write-host "Site URL: $siteURL" -ForegroundColor Cyan Write-host "Site Title: $WebSite" -ForegroundColor Cyan Write-host "Document Library Name: $DocLib" -ForegroundColor Cyan Write-host "Number of Documents: $DocCount" -ForegroundColor Cyan Write-host "Number of Versions: $totalVersion" -ForegroundColor Cyan } else { Write-host "No Documents in $DocLibName" -ForegroundColor Red } } else { Write-host "The version settings is not enabled, please enabel it first" -ForegroundColor Red Start-Sleep 5 [system.Diagnostics.Process]::Start("iexplorer",$siteURL+"_layouts/15/LstSetng.aspx?List="+"") } } catch { Write-host $_.Exception.Message -ForegroundColor Red } } # Provide the SharePoint Site URL, and the Document Library Name Export-DocLibVersions #-siteURL "http://epm:19812/pmo/" -DocLibName "Doc Lib Get Versions Details" |