Living a SharePoint life

Wednesday, January 21, 2015

Code Snippet: Iterating thru all websites

How to iterate thru all SiteCollections and containing the Websites. This script also shows a progressbar on screen:

$sites = Get-SPSite -Limit All
$siteIter = 0
foreach($site in $sites) {
    Write-Progress -PercentComplete (($siteIter / $sites.Count) * 100) -Activity "Iteration Sites" -Status $site.HostName
    $webIter = 0
    
    # Do something here
    
    foreach($web in $site.AllWebs) {
        Write-Progress -PercentComplete (($webIter / $site.AllWebs.Count) * 100) -Activity "Iteration Webs" -Status $web.Title -Id 2
        Write-Host $web.Title
        $webIter++
        
        # Do something here
    }
    $siteIter++
}

Code Snippet: Load the SharePoint PowerShell Module

How do you load the PowerShell module for SharePoint?

Addendum: I've added a try/catch block to the code. This way you can be sure the module is loaded.

# Loading SharePoint Module
try {
    if ((Get-PSSnapin "microsoft.SharePoint.Powershell" -ea silentlycontinue) -eq $null)
    {
        Add-PSSNapin Microsoft.SharePoint.Powershell
    }
}
catch {
    throw "Microsoft SharePoint PowerShell AddIn initialization failed"
    exit 3
}

Featured Post

The Retro Powershell - Looking good in 8-Bit | Part 1

I wrote a little script that, when placed in your PowerShell Profile, will print a message similar to the old boot message you got from you...