Get a SharePoint Publishing Content Inventory using PowerShell


There may be a time when you want to get an inventory of all publishing content (Sites, Documents, Pages, etc.) for an entire site collection. I needed to do exactly that, so I wrote a handy little PowerShell function that walks through a Publishing Site Collection – counting the webs, pages, images and documents.

Here is the function!

function Get-SPPublishingSiteInventory {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true)][string]$SiteUrl
)
    Start-SPAssignment -Global
    $Site = New-Object Microsoft.SharePoint.SPSite($SiteUrl)
	$PubSite = New-Object Microsoft.SharePoint.Publishing.PublishingSite($Site)
	$PubPageCount = 0
	$ImageCount = 0
	$DocCount = 0
	foreach ($Web in $Site.AllWebs) {
        $PubWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($Web)
		$Pages = $PubWeb.GetPublishingPages($PubWeb)
		$PageCount = $Pages.Count
		$PubPageCount = $PubPageCount + $PageCount
		$Images = $PubWeb.ImagesLibrary.ItemCount
		$ImageCount = $ImageCount + $Images
		$Documents = $PubWeb.DocumentsLibrary.ItemCount
		$DocCount = $DocCount + $Documents
		$Web.Dispose()		
    }
    $Site.Dispose()
    Write-Host "Number of Sites: "     $site.allwebs.count
    Write-Host "Number of Pages: "     $PubPageCount
	Write-Host "Number of Images: "    $ImageCount
	Write-Host "Number of Documents: " $DocCount
    Stop-SPAssignment -Global
}
Advertisement

3 thoughts on “Get a SharePoint Publishing Content Inventory using PowerShell

  1. Abhishek Bhattacharya

    Grt script just a little modification
    Slight modification
    ======================

    Param([string]$SiteUrl)
    [System.Reflection.Assembly]::LoadWithPartialName(”Microsoft.SharePoint”)

    function SPPublishingSiteInventory{

    $Site = New-Object Microsoft.SharePoint.SPSite($SiteUrl)

    $PubSite = New-Object Microsoft.SharePoint.Publishing.PublishingSite($Site)
    $PubPageCount = 0

    $ImageCount = 0

    $DocCount = 0

    foreach ($Web in $Site.AllWebs) {

    $PubWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($Web)

    $Pages = $PubWeb.GetPublishingPages($PubWeb)

    $PageCount = $Pages.Count

    $PubPageCount = $PubPageCount + $PageCount

    $Images = $PubWeb.ImagesLibrary.ItemCount

    $ImageCount = $ImageCount + $Images

    $Documents = $PubWeb.DocumentsLibrary.ItemCount

    $DocCount = $DocCount + $Documents

    $Web.Dispose()

    }

    $Site.Dispose()

    Write-Host “Number of Sites: ” $site.allwebs.count

    Write-Host “Number of Pages: ” $PubPageCount

    Write-Host “Number of Images: ” $ImageCount

    Write-Host “Number of Documents: ” $DocCount

    }

    SPPublishingSiteInventory;

    • Ryan Dennis

      Abhishek,
      Thanks for the comment. I’m not sure how your script improves upon mine though. It looks like instead of a reusable function you’ve turned it into an actual script, which would work just fine – but by wrapping my code in the function I’ve made it so I can run it just like a Cmdlet. Typically I load my functions into a module that I pre-load in my PowerShell Profile – then I have these available with comment-based help just like any other Cmdlet.

      Not saying yours isn’t good, I just try to make all of my scripts into reusable functions where it makes sense.

      Thanks again for the comment!
      Ryan

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s