Get a Web, List and Library Inventory using PowerShell


A colleague of mine recently asked me “Ryan, can you give me some PowerShell code that can give me a list of all sites and sub sites as well as all lists and libraries within each of those sites – for an entire web application?”

“Of course”, I said…

I had some other scripts and functions that were similarly constructed, so I simply took one that was close and adapted it to make it work.

This function, which I’ve called “Get-SPSiteInventory” will run against either an entire Web Application (using the -WebApplication switch param) or a single Site Collection (using the -SiteCollection switch param).

I’ve tested this both by sending the output straight to a file (using the Out-File cmdlet) as well as just running in the shell – both work pretty nicely.

I’ve excluded comment-based help for better readability, and the syntax is as follows…

To run against a site collection:

Get-SPSiteInventory -Url http://spsite -SiteCollection

To run against a web application:

Get-SPSiteInventory -Url http://spwebapp -WebApplication

The entire function:

function Get-SPSiteInventory {
Param(
[string]$Url,
[switch]$SiteCollection,
[switch]$WebApplication
)
Start-SPAssignment -Global
	if ($SiteCollection) {
		$site = Get-SPSite $Url
		$allWebs = $site.allwebs
		foreach ($spweb in $allWebs) {
			" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "
			$spweb.Url
			$spweb.Lists | select Title, BaseType
			$spweb.dispose()
		}
		$site.dispose()
	} 
	elseif ($WebApplication) {
		$wa = Get-SPWebApplication $Url
		$allSites = $wa | Get-SPSite -Limit all
		foreach ($spsite in $allSites) {
			$allWebs = $spsite.allwebs
			foreach ($spweb in $allWebs) {
			" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "
			$spweb.Url
			$spweb.Lists | select Title, BaseType
			$spweb.dispose()
			}
		}
	}
Stop-SPAssignment -Global
}

DAYSPUG Presentation December 13th 2011


Last night I spoke to a great crowd of PowerShell Enthusiasts at the Dayton SharePoint Users Group (DAYSPUG) in Dayton, Ohio. I knew most of the folks in attendance from other local SharePoint and PowerShell events, so it was nice to see everyone again.

It was a pleasure to speak in front of all of you, and I thoroughly enjoyed the engagement that you all showed.

Since the actual presentation/demo wasn’t much different from my recent engagement at SharePoint Saturday Cincinnati, I’ll simply point to that post for the actual PowerShell and XML code I used in the demos. That content is located here: SPS Cincinnati Post

Here is a post which contains the two Christmas functions I shared last night as well: Two PowerShell Functions for the Christmas Season

Thanks again to Tony Maddin and the rest of the DAYSPUG members for welcoming me to speak. See you all next time!

RD

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
}

Creating Site Structure using PowerShell and XML


This post is related to another recent post on creating pages using PowerShell and XML, but this time the focus is on creating sites (SPWebs, specifically :)) using PowerShell and XML input. I knew based on my prior experiences with creating Pages that XML was a nice, easy way to automate the tasks. Using the following XML syntax allowed me to put together what would work for my scenario:

<?xml version="1.0" encoding="utf-8"?>
<Sites>
  <Site>
    <SiteTitle>My First Site</SiteTitle>
    <SiteUrl>http://intranet/myfirstsite</SiteUrl>
  </Site>
  <Site>
    <SiteTitle>My Second Site</SiteTitle>
    <SiteUrl>http://intranet/mysecondsite</SiteUrl>
  </Site>
<Sites>

Really pretty simple – right? Now I did have other settings for my Webs, but they were static across all of my sites (Template, for example) so I haven’t included them here. However, you could easily add additional tags and logic to apply different templates, locale IDs, etc…

Once I had the XML, creating the sites was VERY easy. It’s literally just 24 lines of PowerShell code, with the work coming from the ForEach-Object cmdlet. (Note: I excluded my typical comment-based help for ease of reading.)

Without further ado, here is the PoSh code!

function New-SPWebFromXml {
Param([string]$XmlInput)
# Read in list of sites from XML
[xml]$SitesXml = Get-Content $($XmlInput)
if ($SitesXml -eq $null) {return}
Start-SPAssignment -Global
$StartTime = Get-Date
# Loop through each site node to extract data
$SitesXml.Sites.Site | ForEach-Object {
$SiteTitle = [string]$_.SiteTitle
$SiteUrl = [string]$_.SiteUrl
$WebTemplate = "CMSPUBLISHING#0"
$LangId = "1033"
Write-Host "Creating an SPWeb at $($SiteUrl)"
New-SPWeb -Url $SiteUrl `
-Language $LangId `
-Template $WebTemplate `
-Name $SiteTitle
}
$EndTime = Get-Date
$TimeSpan = New-TimeSpan $StartTime $EndTime
Write-Host "$($SitesXml.Sites.Site.Count) sites created in $timespan"
Stop-SPAssignment -Global
}