Friday, 24 September 2010

Easy object disposal in SharePoint 2010 PowerShell scripts

If you’ve done any type of SharePoint development, you’ll know about SPSite and SPWeb object disposal (see here if not). I’m not going to cover the subject again, except to say that you also need to build in disposal practices when using PowerShell scripts for SharePoint to avoid retaining the objects in memory, potentially causing performance issues on your servers.

PowerShell will automatically dispose of objects used in certain types of commands - e.g., those that appear on a single line or as part of a piped command, but you will need to manually dispose of objects for commands where you are instantiating a variable using Get-SPSite or Get-SPWeb. For example, PowerShell will automatically handle object disposal for the following command:

Get-SPWebApplication | Get-SPSite -limit all | ForEach-Object { write-host $_.Url  }

But, as the command below creates a variable called $web based on the SPWeb object, this will require disposal by specifying the $web.dispose() command after it has been used:

$web = Get-SPWeb http://portal
#PowerShell script goes here
#Now dispose of SPWeb object

Now, for all you IT Pro’s out there, this alone may scare you into never using PowerShell to manage SharePoint again (and I wouldn’t blame you). However, help is at hand with the Start-SPAssignment and Stop-SPAssignment cmdlets introduced in SharePoint 2010. To overcome the hassle of having to know when to dispose of an object and when not to, all you need to do is sandwich your script in between the following commands:

Start-SPAssignment –Global

#PowerShell script goes here

Stop-SPAssignment –Global

Any objects defined between the Start-SPAssignment –Global and Stop-SPAssignment –Global commands will be automatically disposed of by PowerShell as it will now know when the script has finished running. So, even if you are not sure whether objects should be disposed of or not, as long as you always top and tail your script with these cmdlets, you can’t go wrong.


  1. This is great. I am now using this all the time. Do you or anyone else know of any drawbacks?

  2. Fantastic! Didn't know about this before.
    Thank you so much!

  3. What if a script execution will abort before reaching the line "Stop-SPAssignment..."?
    Will the next full execution of the script dispose all remaining objects or do I need to execute that command separately after each earlier abortion?

  4. Great article. happy to visit your blog. Thanks for sharing.

    2 and 3 bhk flats in nungambakkam

  5. تعد خدمات الاول في المدينة المنورة من افضل الخدمات في مجال التنظيف ومكافحة الحشرات لانها تتميز بجودة المنظفات المستخدمة وامهر عماله تعمل على انهاء مهامها على اكمل وجه وبنظافه ترضى العميل بالاضافة الى خدمة مكافحة الحشرات ورش المبيدات المتميزة لدينا
    تخزين اثاث بالرياض
    شركة تنظيف شقق بالمدينة المنورة
    شركة تنظيف كنب بالمدينة المنورة
    افضل شركة تنظيف فلل بالمدينة المنورة

    شركة نقل اثاث بالمدينة المنورة
    شركة كشف تسربات المياه بالمدينة المنورة
    شركة مكافحة الحشرات بالمدينة المنورة
    افضل شركة تنظيف خزانات بالمدينة المنورة

    افضل شركة غسيل كنب بالرياض


  6. Wonderful blog! I found it while searching on Yahoo News. Do you have any suggestions on how to get listed in Yahoo News? I’ve been trying for a while but I never seem to get there! Many thanks.
    tanki online 3 | 2048 online