Archive for PowerShell Script

SharePoint Online – List of Sites & Webs


Need to create a PowerShell script that iterates through all SharePoint Online site collections and webs and generate info. regarding site collection size, last time site/web update and other.


Tweaked script by Jose Quinto @

Output is two CSV files in temp folder on C drive root. One with the details and other with site collection on which account gets 401 error.

$Global:webinfo = @()

function AuthenticateUserProfile($siteUrl, $tenantAdmin, $secureAdminPassword)
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($tenantAdmin, $secureAdminPassword)
$ctx.Credentials = $credentials
return $ctx

function RecursiveWebs($ctx, $web){

foreach($w in $web.Webs)

$Properties = @{
Title = $w.Title
URL = $w.Url
NoOfSubsites = $w.Webs.Count
LastUserModifiedDate = $w.LastItemUserModifiedDate
LastItemModifiedDate = $w.LastItemModifiedDate
SiteCollection = ‘No’
SiteOwner = $w.Author.Title

$Global:webinfo += New-Object psobject -Property $properties

if ($w.Webs.Count -gt 0)
RecursiveWebs $ctx $w

$username = “”
$password = ConvertTo-SecureString “PASSWORD” -AsPlainText -Force
$O365Credential = New-Object System.Management.Automation.PsCredential($username, $password)

#SharePoint Online Admin site URL
Connect-SPOService -Url -Credential $O365Credential
$sites = Get-SPOSite -Limit All -Detailed

foreach ($site in $sites)
$site.Url.Equals(“”) -or

Write-Host $site.Url
$ctx = AuthenticateUserProfile $site.Url $username $password;
$web = $ctx.Web

$Properties = @{

Title = $site.Title
URL = $site.Url
SiteSizeLimitInMB = $site.StorageQuota
SiteUsageInMB = $site.StorageUsageCurrent
NoOfSubsites = $web.Webs.Count
SiteOwner = $site.Owner
LastContentModifiedDate = $site.LastContentModifiedDate
LastUserModifiedDate = $web.LastItemUserModifiedDate
SiteCollection = ‘Yes’
LastItemModifiedDate = $web.LastItemModifiedDate

$Global:webinfo += New-Object psobject -Property $properties

if ($web.Webs.Count -gt 0)
RecursiveWebs $ctx $web
Write-Host $_ -ForegroundColor Red
$Properties1 = @{

Title = $site.Title
URL = $site.Url


$site401 += New-Object psobject -Property $properties1



$webinfo | Select-Object Title, URL, SiteSizeLimitInMB, SiteUsageInMB, LastItemModifiedDate,
LastUserModifiedDate, LastContentModifiedDate, NoOfSubsites,
SiteOwner, SiteCollection | Export-Csv -notypeinformation -Path ‘C:\temp\SPOinfo.csv’

$site401 | Select-Object Title, URL | Export-Csv -notypeinformation -Path ‘C:\temp\SPOinfo401.csv’



Comments (1)

SharePoint 2013 Migration: User not able to access after migration from SharePoint 2010 to SharePoint 2013


We have migrated our sites from SharePoint 2010 to SharePoint 2013. After migration, users are not able to access newly migrated site. If we delete user and provide access again, user able to access site. (Fortunately, this was happening on test migration)


The missing link was warning that we overlooked. When we ran PowerShell Test-SPContentDatabase cmdlet, we missed following warning:

Category : Configuration

Error : False

UpgradeBlocking : False

Message : The [SharePoint2013] web application is configured with claims authentication mode however the content database you are trying to attach is intended to be used against a windows classic authentication mode.

Remedy : There is an inconsistency between the authentication mode of target web application and the source web application. Ensure that the authentication mode setting in upgraded web application is the same as what you had in previous SharePoint 2010 web application. Refer to the link for more information.

Locations :

In other words, SharePoint 2013 discourage classic mode authentication. If web application is created via Central Administration, claim based is the default and preferred method of authentication. If want to create web application with classic mode authentication, you need to use PowerShell cmdlets.

It goes like this:

  1. Create classic mode web application using PowerShell cmdlets
  2. Attach all content database with web application
  3. Convert classis mode web application to claim base authentication
  4. Configure Object cache

Create classic mode web application using PowerShell cmdlets

$ap = New-SPAuthenticationProvider -UseWindowsIntegratedAuthentication –DisableKerberos

New-SPWebApplication -Name “SharePoint – 2013” -ApplicationPool “SharePoint 2013 Web Apps” -ApplicationPoolAccount (Get-SPManagedAccount “domain\login”) -Port 80 -Url http://sharepoint2013 -AuthenticationMethod NTLM -AuthenticationProvider $ap -DatabaseName “WSS_Content_01”

Attach all content database with web application

Mount-SPContentDatabase “WSS_Content_2” -DatabaseServer “SQLDB2013” -WebApplication http://sharepoint2013

Convert classis mode web application to claim base authentication

Convert-SPWebApplication –Identity http://sharepoint2013 –To Claims -RetainPermissions –Force

Configure Object cache

$wa = Get-SPWebApplication -Identity http://sharepoint2013

$wa.Properties[“portalsuperuseraccount”] = “domain\login”

$wa.Properties[“portalsuperreaderaccount”] = “domain\login”


Make sure you enter user login using SharePoint 2013 claims encoding. It must be in this format “i:0#.w|contoso\chris” [For details, please visit] and don’t forget to restart IIS.

Leave a Comment

SharePoint 2010: PowerShell script to add in web.config


We need to add an entry in “<assemblies>” section of the “web.config” file of specific web application using power shell script


Param ( [Parameter(Mandatory=$true, ValueFromPipeline=$true, Position=0)]




$WebApp = $WebApplication.Read()

Write-Host $WebApp

$configMod = New-Object Microsoft.SharePoint.Administration.SPWebConfigModification

$configMod.Name = “add[@name=””assembly””]”

$configMod.Path = “/configuration/system.web/compilation/assemblies”

$configMod.Value = “<add assembly=””AjaxControlToolkit, Version=3.0.30930.28736, Culture=neutral, PublicKeyToken=28f01b0e84b6d53e”” />”

$configMod.Sequence = 0

$configMod.Type = 0 #for enum value of SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode

$configMod.Owner = “Ajax”





Comments (1)

SharePoint 2010: PowerShell Script to add / display list as webpart


We were required to add / display discussion forum / discussion board as a webpart on a publishing page.


We used “XsltListViewWebPart” webpart to accomplish above requirement

$webpart New-Object Microsoft.SharePoint.WebPartPages.XsltListViewWebPart

$webpart.ListId $list.ID;

$webpart.ViewGuid $list.DefaultView.ID.ToString();

$webpart.AllowClose $false;

$webpart.AllowConnect $false;

$webpart.AllowEdit $false;

$webpart.AllowHide $false;

$webpart.AllowMinimize $false;

$webpart.AllowZoneChange $false;

$webpart.ChromeType = [System.Web.UI.WebControls.WebParts.PartChromeType]::None;




$webpartmanager.AddWebPart($webpart, “Zone 1”, 0);

You need to set following variables:

$list = Get the reference of list. In our case, it was discussion forum

$pageUrl = Url of the page

Leave a Comment

SharePoint 2010: PowerShell Script to create Discussion Board / Discussion Forum


We need to create discussion board / discussion forum using power shell script


$spWeb Get-SPWeb $webURL

$listTemplate = [Microsoft.SharePoint.SPListTemplateType]::DiscussionBoard

$spWeb.Lists.Add(“Discussion Forum”, “Custom List”, [int]$listTemplate)

Thanks to my friend Abdur Raheem 

Comments (1)

PowerShell Script: Enable and Add Content Type in Document Library


We created content types using power shell script. Now, we need to enable content types and add these content types in the list.


$site = Get-SPSite “url of site collection”

$rootWeb $site.RootWeb

$docLib $rootWeb.Lists[“Shared Documents”]

$customContentTypes “ContentType1,ContentType2,ContentType3”

$docLib.ContentTypesEnabled $true


$customContentTypeArray $customContentTypes.Split(“,”)

foreach($customContentType in $customContentTypeArray)


    $customCT $rootWeb.ContentTypes[$customContentType]




Comments (3)

PowerShell Script: Change Site Theme


We have customized a site theme as explained in previous post. Now, we need to apply it on Site and all its webs.


$name = [Name of theme]

$filepath = [Path of the theme]

$bytes = [System.IO.File]::ReadAllBytes($filepath + “\” $name);

$site = Get-SPSite(“url of site collection”)

$rootWeb $site.RootWeb

$list $rootWeb.Lists[“Theme Gallery”]


$theme = [Microsoft.SharePoint.Utilities.ThmxTheme]::Open


foreach($web in $site.AllWebs)




Leave a Comment

Older Posts »