SPDocVersion-Exporter.ps1
<#PSScriptInfo .VERSION 1.7 .GUID f47abff3-8d54-4a9d-bac5-a558ae0fb43a .AUTHOR Mohamed El-Qassas .COMPANYNAME debug.to .COPYRIGHT This project is licensed under the terms of the MIT license. .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 Add export option for a particular document .PRIVATEDATA #> <# .DESCRIPTION The "SPDocVersion-Exporter" is a well-written and tested PowerShell script that helps you to easily export all document versions details for a specific SharePoint document library to Excel/CSV file. 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 { #################################################################### $WebSite = $null $DocLib = $null #perfrom validation Write-Host "#####################################`n SharePoint Doc Library Versions Exporter`n#####################################" -ForegroundColor Green if ((Get-SPWeb $siteURL -ErrorAction SilentlyContinue) -eq $null) { Write-Host "The provided SharePoint Site URL ($siteURL) is not found, Please, provide a valid SharePoint URL!" -ForegroundColor Red Export-DocLibVersions } else { $WebSite = Get-SPWeb $siteURL #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 } else { #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 #Get all documents $DocsCollection = $DocLib.Items $DocCount = $DocsCollection.Count Write-Host ">> The total number of documents in ($DocLibName) is ($DocCount)" -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 } if ($DocsCollection -ne $null) { $reply = $(Write-Host "Would you like to export all documents versiosn or specic document?`n[Type All to export versions for alldocument/or type the full document URL]" -ForegroundColor yellow; Read-Host) Write-Host "Start Exporting process ......." -ForegroundColor cyan if ($reply -eq "All") { Write-Host "The total number of documents in ($DocLibName) is ($DocCount)." -ForegroundColor Cyan #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 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 { $DocURL = $reply try { $Doc = $WebSite.GetListItem($DocURL) if ($Doc -ne $null) { $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 $count = $Vcount #Set the decremental count to the Version Count #Iterate for each version in each document 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 } Write-Host "The versions of $DocName have been exported successfuly at $ReportFilePath" -ForegroundColor Green Start-Process $ReportFilePath 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 "Document Name: $DocName" -ForegroundColor Cyan Write-Host "Number of Versions: $Vcount" -ForegroundColor Cyan } } catch { Write-Host "The Document URl ($reply) is not found, Please type valid document URL" -ForegroundColor Red Export-DocLibVersions } } } 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 $EnableVersioning = $siteURL + "_layouts/15/LstSetng.aspx?List=" + $DocLib.ID [system.Diagnostics.Process]::Start("iexplore",$EnableVersioning) } } } } 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" -Document URL http://epm:19812/pmo/Doc%20Lib%20Get%20Versions%20Details/Doc%20Lib%20Get%20Versions%20Details.txt |