Jean Paul's Blog

There are 2 types of People in the World, One who Likes SharePoint and..

  • Microsoft MVP

  • MindCracker MVP

  • CodeProject MVP

  • eBook on SharePoint 2010

  • eBook on Design Patterns

  • eBook on Windows Azure

  • NLayers Framework @ CodePlex

  • MSDN Forums

  • .Net vs. Java

    Due to Public Demand
  • Advertisements

Posts Tagged ‘powershell’

PowerShell Memory Leak & Prevention

Posted by Paul on May 13, 2015

In this article we can explore how to prevent memory leaks while writing SharePoint scripts.

Memory Leak

While writing PowerShell scripts, there are immense chances to form memory leaks if the script-writer did not free the objects correctly. While dealing with SPFarm, SPSiteAdministration SPSite, SPWeb objects with huge internal data, the problem can become more vulnerable crunching system resources.


We can prevent memory leaks quickly using the following Cmdlets:

1. Start-SPAssignment

2. Stop-SPAssignment

Start- SPAssignment & Stop-SPAssignment

Start-SPAssignment will start collecting memory objects. Later we can invoke the Stop-SPAssignment to release the memory.

$c = Start-SPAssignment;

$w = Get-SPWeb –Site http://serfver –AssignmentCollection $c

Stop-SPAssignment $c;

Explanation In the above code, we are first declaring the variable $c pointing to collect the assignment objects. In the next line Get-SPWeb will create a web object in $w and simultaneously it I added to the assignment collection. The Stop-SPAssignment will release all the memory-objects within $c.


Here additional coding is required to add the web objects to assignment collection. In production scenarios where pre-existing PowerShell script has to be modified will required much effort.

Using –Global Switch

We can do memory leak prevention quickly using the –Global switch. Example is given below:

Start-SPAssignment -Global

$w = Get-SPWeb –Site http://serfver –AssignmentCollection $c

Stop-SPAssignment -Global

Types of Assignment

There are 3 types of Assignments:

1. No Assignment

2. Simple Assignment

3. Advanced Assignment

Simple Assignment is the one we have seen using the –Global parameter. This is used more often.

Advanced Assignment is the one with assignment variable. This is used in advanced scripts.

No Assignment is where no parameters or variables are used. Here objects are disposed each iteration.



In this article we have explored how to prevent memory leaks while writing SharePoint scripts.


Posted in SharePoint, SharePoint 2013 | Tagged: , , , , | Leave a Comment »

Create Site Collection with New Managed Path, Content Database

Posted by Paul on July 14, 2013

In this article I would like to share the PowerShell script which performs the following:

1. Create new content database

2. Create new managed path

3. Create new site collection

PowerShell Script

Following is the PowerShell Script which performs the same.

# Add Snapin

If ((Get-PSSnapIn -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null )


Add-PSSnapIn -Name Microsoft.SharePoint.PowerShell


# Create Content Database

New-SPContentDatabase -name “DATABASE-NAME” -WebApplication “http://WEB-APP-URL”

# Create Managed Path

New-SPManagedPath -RelativeURL “path/subpath” -WebApplication “http://WEB-APP-URL” -Explicit

# Create Site Collection

New-SPSite http://WEB-APP-URL/path/subpath -OwnerAlias DOMAINuser -Name “Site Collection Title” -Template “STS#0” -ContentDatabase “DATABASE-NAME”


You can verify the new site using the URL:


You can verify new managed path through Central Administration >Main Page > Select web application > Managed Paths (toolbar item)


You can verify the new content database through Central Administration > Main Page > Manage Content Databases link.



The same PowerShell script will be added to PowerShell Repository.  Please give me some time to do it.


In this article we have explore the PowerShell script to create new site collection with new managed path & content database.  I hope this will be useful for site conversion activities.

Posted in SharePoint, SharePoint 2010 | Tagged: , , , , , | Leave a Comment »