PowerShell Scripts

During my current project we are using a lot of PowerShell scripts to administrate the environment and make development easier for us.

In this post that I will try to update frequently, I will share some of our useful PowerShell scripts.

Unregister a Custom Claim Provider for a specific zone in a web application

There are situations were for you will develop custom claim providers. These claim providers can be installed globally on the farm but can also be installed for a specific zone in a web application. The following script will allow you to unregister a custom claim provider for a zone.

function Unregister-CustomClaimProvider{
		  [string] $DisplayName = $(Read-Host -prompt "Claim Provider Display Name"),
		  [string] $Url = $(Read-Host -prompt "Web Application Identity"),
		  [string] $InternalName = $(Read-Host -prompt "Claim Provider Internal Name"),
		  [string] $Zone = $(Read-Host -prompt "Zone")

	$WebApplication = Get-SPWebApplication $Url 
	if ($WebApplication.IisSettings.ContainsKey($Zone)){
		$settings = $WebApplication.GetIisSettingsWithFallback($zone)
		$providers = $settings.ClaimsProviders;
		if($providers.Contains($InternalName)) {
			Write-Host "Claim Provider is registerd in this zone trying to remove..." -ForegroundColor Green
			Set-SPWebApplication -Identity $WebApplication -Zone $Zone -AdditionalClaimProvider $providers
			Write-Host "$DisplayName is not registered on $($WebApplication.Url) in zone $Zone" -ForegroundColor Yellow

Disable Browser Based Editing for Sites under a Specific Url

In certain situations you would like to disable all browser based editing for specific sites within SharePoint. With the below PowerShell script you can disable browser based editing.

Get-SPSite -WebApplication [Web Application] -Limit ALL | where {$_.Url.StartsWith("[Url start With]") } | Foreach-Object { 
    Write-Host "Activating Feature Client Based Editing for site" $_.Url
    Enable-SPFeature –identity 8a4b8de2-6fd8-41e9-923c-c7c3c00f8295 -URL $_.Url

Warm Up SharePoint 2013 Site Collections

When you maintain SharePoint environments you would like to warm up the environments for example once a day. The first load on SharePoint still takes a lot of time. For this situation I have developed a warm up script that also take in account your extended Host Named Site Collections.

function WarmUp-Url([string]$url){
    Write-Host "Warming up " $url -ForegroundColor DarkYellow;
    Invoke-WebRequest $url -UseDefaultCredentials -UseBasicParsing; 

foreach ($WebApp in (Get-SPWebApplication -IncludeCentralAdministration)){
    $sites = $WebApp | Get-SPSite -Limit All | Get-SPWeb -Limit All
        foreach($site in $sites){
            $html= WarmUp-Url -url $site.Url 
                $extendedurls = Get-SPSiteURL $site.Url  -ErrorAction SilentlyContinue;

                foreach($url in $extendedurls){
                    if($url.Url -ne $site.Url){                        
                        $html = WarmUp-Url -url $url.Url;

Publish Content Types for Specific group

When you start working with the Content Type Hub in SharePoint you will get in situations were you would like to publish multiple content types at once. In these situations you could use the below function to publish all the content types in a specific group.

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
function Publish-ContentTypesForGroup {

    $site = Get-SPSite $hubUrl

    if(!($site -eq $null))
        $contentTypePublisher = New-Object Microsoft.SharePoint.Taxonomy.ContentTypeSync.ContentTypePublisher ($site)
        $site.RootWeb.ContentTypes | ? {$_.Group -match $group} | % {

                Write-Host "Content Type" $_Name " is Read Only" -ForegroundColor Yellow;
                $_.ReadOnly = $false;
            write-host "Content type" $_.Name "has been republished" -foregroundcolor Green

Starting the Content Type Subscriber Job

When you work a lot with the Content Type Hub in SharePoint you would also like to start the Content Type Subscriber job by using PowerShell. The below script does the job the web application you specify.

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
function Run-SPContentTypeHubJob([string]$url) 
    $job = Get-SPTimerJob -WebApplication $url | ?{ $_.Name -like "MetadataSubscriberTimerJob"} 

    if ($job -ne $null)  
        Write-host "The timerjob will be started....." -ForeGroundColor Green
        $job | Start-SPTimerJob 

        Write-host  "Run the admin job..." -ForeGroundColor Green
        Start-SPAdminJob -ErrorAction SilentlyContinue              
	Write-Host "The Job could not be found" -ForeGroundColor Red

Delete all items in a List

During many projects I had situations were I had to remove all items in a list. In this situation you can choose for removing the complete list en recreating it or you could delete every single item. With the below function you can delete every item in the list.

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
function Clear-SPList {

    $site = Get-SPSite $siteUrl;
    $web = $site.OpenWeb();
    $list = $web.Lists[$listname];

    write-host "Clearing SharePoint list: " $list.Title -ForeGroundColor Green

    $collListItems = $list.Items;
    $count = $collListItems.Count - 1;
    for($intIndex = $count; $intIndex -gt -1; $intIndex--)

Finding content types that use a specific field

It ofton occurs that you would like to delete a site column but can’t because it is used by a specific content type. It really takes a long time then to find the right content type. Using the below PowerShell function it is easy and fast to find your content type.

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
function Find-ContenttypesByField {

  $site = Get-SPSite $siteUrl
  $web = $site.RootWeb;
  $fields = $web.Fields;

  $guid = $fields[$fieldname].id;

  $ct = $web.AvailableContentTypes 
  for ($i=0; $i -lt $ct.count; $i++) 
    for ($j=0; $j -lt $ct[$i].fields.count; $j++) 
      if ($ct[$i].fields[$j].id -eq $guid)
        Write-host $ct[$i].Name " has column";

Setting up Ping and SharePoint Trust Federation

For a environment we wanted to setup a SSO environment with for example K2. The most know situation is using ADFS. But at our client we were required to use Ping. The configuration an the SharePoint site looks very much the same as ADFS. For a reference I have included the PowerShell script in this post.

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

$certpath  = "[Ping Certification Path]"
$stsname   = "Auth-iLink"
$stsdesc   = "PingFederate Claims Provider SharePoint"
$stsrealm  = "sharepoint:ping:con"  #realm configured on Ping
$signinurl = "[URL Ping]/idp/prp.wsf"

Write-host "Importing Certification on SharePoint Server (Trust relation)." -ForegroundColor Green
New-SPTrustedRootAuthority -Name "PingFederate IP-STS Sharepoint" -Certificate $certpath

Write-Host "Getting the Certification Object." -ForegroundColor Green
$cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($certpath)

Write-Host "Setting up Claim Mapping for SharePoint Authentication" -ForegroundColor Green
$mappingRole = New-SPClaimTypeMapping -IncomingClaimType "http://schemas.xmlsoap.org/claims/Group" -IncomingClaimTypeDisplayName "Role" -LocalClaimType "http://schemas.microsoft.com/ws/2008/06/identity/claims/role"
$mappingName = New-SPClaimTypeMapping -IncomingClaimType "http://schemas.xmlsoap.org/claims/CommonName" -IncomingClaimTypeDisplayName "GivenName" -LocalClaimType "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname"

Write-Host "Setting Up Token Issuer"
New-SPTrustedIdentityTokenIssuer -Name $stsname -Description $stsdesc -Realm $stsrealm -ImportTrustCertificate $cert -ClaimsMappings $mappingRole,$mappingName -SignInUrl $signinurl -IdentifierClaim $m2.InputClaimType

Write-Host "Trusted Identity Token Issuer is set up."

Delete empty sub folders

For a migration we had a large set of folder with also empty folders in it. During the migration we iterated trough the folders also going trough the empty folders took a lot of time for the migration process. That way we wanted a PowerShell script to delete the empty sub folders. (Remove the –whatif to actually perform the script).

Get-ChildItem -recurse | Where {$_.PSIsContainer -and `
@(Get-ChildItem -Lit $_.Fullname -r | Where {!$_.PSIsContainer}).Length -eq 0} |
Remove-Item -recurse -whatif


Delete site that start with a specific URL.

When you are using host named site collections within your environment and also build some kind provisioning mechanism (how this can be done with host named site collections can be read here). You will get in situations that you would like to remove a huge number of site collections that start with a specific URL. By using this script you can:

Get-SPSite -Limit ALL | where {$_.Url.StartsWith("http://spdev.com/sites") } | Foreach-Object { Remove-SPSite $_.Url –Confirm:$False

Flushing the blob cache

Because we are using the blob cache within our environment, we also need a easy option to clear the blob cache when we have update our style sheets or maybe JavaScript files.

Using this function you can easily clear the blob cache for a specific web application.

function Flush-SPBlobCache{
          [string] $Url = $(Read-Host -prompt "Web Application Identity")
        $webApp = Get-SPWebApplication $Url
        Write-Host "Flushed the BLOB cache for:" $webApp


Feature upgrading

For upgrades on our solutions we use the standard upgrading framework of SharePoint. You can read about it here this is an article on the upgrading framework for SharePoint 2010 but it is also something for SharePoint 2013.

Using the following function you are able to upgrade a specific feature within a web application.

function Upgrade-SpecificFeatures{
          [string] $Url = $(Read-Host -prompt "Web Application Identity"),
          [Guid] $ID = $(Read-Host -prompt "Feature ID")

        $SPWebApp = Get-SPWebApplication $Url;

        foreach ($site in $SPWebApp.Sites){
            if ($site -ne $null){

                Write-Host "Upgrading site features for site:" $site -ForegroundColor Green;
                $count = 0;
                $features = $site.QueryFeatures($ID, $true);
                Foreach($feature in $features) {
                        $oldVersion = $feature.Version;
                        Write-Host "Upgrading feature:" $feature.Definition.DisplayName;
                        Write-Host "  Current version:" $oldVersion;
                        $newVersion = $feature.Version;
                        Write-Host "      New version:" $newVersion;
                        if ($oldVersion -eq $newVersion) {
                            Write-Host "  Something went wrong upgrading this feature!" -ForegroundColor Red;
                        } else {
                            Write-Host "  Upgrade successful." -ForegroundColor Green;

            if ($count -eq 0) {
                Write-Host "No features found that needed upgrading for site:" $site -ForegroundColor Yellow;
            } else {
                Write-Host "Done upgrading" $count "feature(s). for site:" $site -ForegroundColor Green;
Share Button

Leave a Reply

Your email address will not be published. Required fields are marked *

nine − = 6

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>