0

I want to use PowerShell to loop through my SharePoint and dispose all objects properly.

I don´t want to generate entrys in the SharePoint log like this:

An SPRequest object was not disposed before the end of this thread. To avoid wasting system resources, dispose of this object or its parent (such as an SPSite or SPWeb) as soon as you are done using it. Due to flags specified at object creation, this will not be freed until processed by garbage collection.

Is the following PowerShell code ok?

$WebApps = Get-SPWebApplication

foreach($WebApp in $WebApps)
{
    $Sites = $WebApp.sites 

            foreach($Site in $Sites)
            {
                $Webs = $Site.AllWebs 

                    foreach($Web in $Webs)
                    {
                    Write-Host "Url: " $Web.url
                    $Web.Dispose()  
                    }
                $Site.Dispose()    
            } 
}
$WebApps = $Null

1 Answer 1

1

Seems like you are never even opening up any SPWeb or SPSite objects in this code, so there should be no need to dispose.

Most common cases when you need to dispose is when you use New-SPWeb or New-SPSite to be able to reach all information in and about the object. Then you should dispose, just as you do in your code example

1
  • Thanks for your answer Robert! When I´m exceuting this code, there are entries in the SP log like: PowerShell_ISE.exe Performance An SPRequest object was reclaimed by the garbage collector instead of being explicitly freed. To avoid wasting system resources, dispose of this object or its parent (such as an SPSite or SPWeb) as soon as you are done using it... Why is this message in the log files?
    – LaPhi
    Commented Mar 28, 2013 at 16:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.