Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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
share|improve this question
add comment

1 Answer

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

share|improve this answer
    
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 Mar 28 '13 at 16:09
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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