Monday, 21 April 2014

PowerShell object Disposing

PowerShell object Disposing
By default powershell will dispose some of the objects..for some type of commands..
Ex:
Get-SPWebApplication | Get-SPSite -limit all
But we will need to manually dispose of objects for commands where we are instantiating a variable using Get-SPSite or Get-SPWeb
Ex:
$web = Get-SPWeb http://SP2010 #Create a object
$web.dispose() #Now dispose of SPWeb object
So everytime we have to call dispose() when you create new object in Powershell...
It will be tedious work when to dispose of an object and when not to.
Don't worry..SharePoint 2010 has provided some cmdlets...
Start-SPAssignment and Stop-SPAssignment
Just use above cmdlets in your powershell script it will take care of object Disposing
How to use Start-SPAssignment and Stop-SPAssignment
Ex:
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