Thursday, October 9, 2014

SharePoint Powershell Scripting: Iterating through a Site Collection to find all BLOBs that havent been modified in over a year

This request came about during a SharePoint/Metalogix StoragePoint requirements gathering session with a customer.  They wanted to know what the storage savings would be if they externalized ONLY files that havent been modified in over a year vs all files.  Metalogix has a great tool affectionately called the "BLOBulator" which will analyze Content DBs (and drill down further into Site Collections), however as a free tool, there isnt much customization when it comes to filtering in criteria for said externalizations.

So I decided to use Powershell to iterate through a site collection of my choosing and output into a csv file. I could then do a quick calculation on all the documents to get a total size.

This script works fine, except at the end it will throw an error on the dispose function.  If anyone can help resolve that, please feel free to leave a comment and I'll update my post.  This script can also be adjusted to run against an entire we application as well.

Add this code to a text document and rename it with a .ps1 extension:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue 

function Get-OldDocuments([string] $SiteURL)

{

    $Site = Get-SPSite $SiteURL


                foreach ($SPWeb in $SPSite.AllWebs)

                   {

                    foreach ($SPList in $SPWeb.Lists) 

                      {

                        # Get Document Libraries

                        if ($SPList.BaseType -eq "DocumentLibrary") 

                         {

                            foreach ($item in $SPList.Items)

                              {

                                $data = @{

                                "Site" = $SPSite.Url

                                "Web" = $SPWeb.Url

                                "list" = $SPList.Title

                                "Item URL" = $item.Url

                                "Item Title" = $item.Title

                                "Item Created" = $item["Created"]

                                "Item Modified" = $item["Modified"]

                                "Size (KB)" = $item.File.Length/1KB

                                "Size (MB)" = $item.File.Length/1MB

                                }


                                # add files older than 1yr old

                                $expireDate = Get-Date 
$expireDate = $expireDate.AddYears(-1)

                                if($item["Modified"] -lt $expireDate)

                                {

                                    Write-Host($SPSite.Url +"/"+ $item.Url)

                                    New-Object PSObject -Property $data

                                }

                            }

                        }
}
                     $SPWeb.Dispose();

                    }

                  $SPSite.Dispose()   

                }


#call the function 

Get-OldDocuments "http://sharepoint.com/site" | Export-Csv -NoTypeInformation -Path C:\temp\OldDocuments.csv



No comments:

Post a Comment