Tuesday, June 11, 2013

SharePoint 2007 - PowerShell to Get All Versions of a Document

This script will get details(URL, Size, Created by, Created Date, Attachment Size etc) of all the versions of the items/document in a list/library.
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

$SiteName = "http://MyDomaon.com/sites/portal"
$LogPath="C:\Temp"
$LogName="GetAllDocumentVersions.txt"

$Logfile = $LogPath + "\" + $LogName
New-Item -ItemType directory -Path $LogPath -Force -ErrorAction SilentlyContinue

#Check if file exists and delete if it does
If((Test-Path -Path $Logfile))
{
    Remove-Item -Path $Logfile -Force
}
 
#Create file and start logging
New-Item -Path $LogPath -Value $LogName –ItemType File -ErrorAction SilentlyContinue

Function LogWrite
{
   Param ([string]$logstring)

   Add-content $Logfile -value $logstring

}

$tempHeader = "ID`tTitle`tList/Lib URL`tDoc/Item Name`tVersion URL`tVersion`tSize(KB)`tCreated Date`tCreated By`tAttachment`tAttachment Size"
LogWrite $tempHeader
    
$site = New-Object Microsoft.SharePoint.SPSite($SiteName)
$spWeb = $site.OpenWeb()
foreach($List in $spWeb.Lists)
{

if(($List.BaseType -eq "GenericList") -and ($List.Hidden -eq $false))
{
    write-host "Processing: " $List.BaseType - $List.Title
    foreach($Item in $List.Items)
    {
        Try
        {
            [long]$AttachmentSize = 0
            $listItemAttachments = $Item.Attachments 
            foreach($file in $listItemAttachments) 
            {            
                $AttachmentSize +=  $file.Length 
            }
        }
        Catch [System.Exception]
        {
            $msg = "Error in getting attachment. ID:" + $Item.ID.ToString()
            #Write-Host $msg
            LogWrite $msg
        }
        
        foreach($version in $Item.Versions)
        {
            Try
            {
                #$createdBy = $version.CreatedBy.LoginName.split("#")
                $createdBy = $version.CreatedBy
                $tempList = $Item.ID.ToString() + "`t" + $version.ListItem.Title + "`t" + $SiteName + "/" + $List.RootFolder.Url+ "`tDispForm.aspx?ID=" + $Item.ID +"`t"+ $SiteName +"/" + $version.Url + "`t" + $version.VersionLabel + "`t" + $AttachmentSize + "`t" + $version.Created + "`t" + $createdBy + "`t" + $version.ListItem.Attachments + "`t" + $AttachmentSize
                #write-host $tempList
                LogWrite $tempList
            }
            Catch [System.Exception]
            {
                $msg = "Error in getting version. ID:" + $Item.ID.ToString() + " URL: " +  $SiteName +"/" + $version.Url
                #Write-Host $msg
                LogWrite $msg
            }
        }
    }
}

if(($List.BaseType -eq "DocumentLibrary") -and ($List.Hidden -eq $false))
{
    write-host
    write-host "Processing: " $List.BaseType - $List.Title
    
    foreach($Item in $List.Items)
    {
        #Get Published Version
        Try
        {
            $tempLib = $Item.ID.ToString() + "`t" + $Item.File.Title + "`t" + $SiteName + "/" + $List.RootFolder.Url+ "`t" + $Item.Name +"`t"+ $SiteName +"/" + $Item.Url + "`t" + $Item.File.UIVersionLabel + "`t" + $Item.File.length/1KB + "`t" + $Item.File.TimeLastModified + "`t" + $Item.File.Author + "`t" + "NA" + "`t" + "NA"
            #Write-Host $tempLib
            LogWrite $tempLib
        }
        Catch [System.Exception]
        {
            $msg = "Error in getting version. ID:" + $Item.ID.ToString() + " URL: " + $SiteName +"/" + $version.Url
            #Write-Host $msg
            LogWrite $msg
        }

        foreach($version in $Item.File.Versions)
        {
            #Get All Historical Versions
            Try
            {
                #write-host $version.ID + "`t" + $version.File.Title + "`t" + $version.Url + "`t" + $version.VersionLabel + "`t" + $version.Size + "`t" + $version.Created + "`t" + $version.CreatedBy+ "`t" + NA + "`t" + NA 
                #$createdBy = $version.CreatedBy.LoginName.split("|")
                $createdBy = $version.CreatedBy
                $tempLib = $Item.ID.ToString() + "`t" + $version.File.Title + "`t" + $SiteName + "/" + $List.RootFolder.Url+ "`t" + $Item.Name +"`t"+ $SiteName +"/" + $version.Url + "`t" + $version.VersionLabel + "`t" + $version.Size/1KB + "`t" + $version.Created + "`t" + $createdBy + "`t" + "NA" + "`t" + "NA"
                #write-host $tempLib
                LogWrite $tempLib
            }
            Catch [System.Exception]
            {
                $msg = "Error in getting version. ID:" + $Item.ID.ToString() + " URL: " + $SiteName +"/" + $version.Url
                #Write-Host $msg
                LogWrite $msg
            }
        }
    }
}

}

$spWeb.Dispose()

Write-Host
Write-Host "Press any key to continue..."
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

Official SharePoint Documentation

I have recently contributed to the official SharePoint documentation for developement. Check it out here: https://docs.microsoft.com/en-us...