Oh my, as I read through Zach’s post, it hit me like a ton of bricks. Not really, but I have been stuck on lyrics lately. The post was very eye opening and one that you need to review if you are using PowerShell and SharePoint. Heck, if you are using PowerShell and any .Net objects that should be Disposed.

“There was a potential for PowerShell users to constantly leak memory without knowing it!“

 

Compare the following 3 lines of PowerShell Script

PS>  $S = New-Object Microsoft.SharePoint.SPSite(“http://sharepoint”)

PS>  $S.Title

My SharePoint Site

PS> $S.Dispose()

 

to

PS>  $S = New-Object Microsoft.SharePoint.SPSite(“http://sharepoint”);  $S.Title; S.Dispose()

 

In the first example there are a total of 3 threads spun up as each PS> will start a new thread. Keep an eye on Zach’s blog for updated work arounds.

 

SharePoint+PowerShell is (Usually) a Perfect Match - Zach Rosenfield's SharePoint Blog

 

Don’t forget to take a look at the updated information on Developer Focused SharePoint Best Practices (Roger Lamb’s blog is a great resource for this, link below)

  1. Common Coding Issues When Using the SharePoint Object Model http://msdn.microsoft.com/en-us/library/bb687949.aspx
  2. Best Practices: Using Disposable Windows SharePoint Services Objects http://msdn.microsoft.com/en-us/library/aa973248.aspx
  3. SPDisposeCheck Code Review utility for SharePoint developers to help prevent memory leaks in their custom code http://code.msdn.microsoft.com/SPDisposeCheck 
  4. Dispose Patterns By Example http://blogs.msdn.com/rogerla