I built a powershell script that connects to sharepoint and adds items to a list. This script caused the PowerScript ISE editor to stop responding and crash before disposing.

The SP server logs display an unexpected error stating "Detected use of SPRequest for previously closed SPWeb object. Please close SPWeb objects when you are done with all objects obtained from them, but not before."

I think previous open SPWeb object is from the crashed instance, how do I know for sure and how do I close all open SPWeb object instances? And how do I know which instance is the one I opened upon crashing?

  • Try closing powershell via the task manager...
    – BlueBird
    Dec 17, 2013 at 13:08

2 Answers 2


just like c# when you open a new spweb or spsite object you need to dispose of it once complete! powershell doesnt have a using statment but some has done it for you ;)

function Using {
    param (
        [System.IDisposable] $inputObject = $(throw "The parameter -inputObject is required."),
        [ScriptBlock] $scriptBlock = $(throw "The parameter -scriptBlock is required.")

    Try {
    } -Finally {
        if ($inputObject -ne $null) {
            if ($inputObject.psbase -eq $null) {
            } else {

# Short example ... 
Using ($user = $sr.GetDirectoryEntry()) { 
  $user.displayName = $displayName 


there is no way that i know of to know within the pipeline to which spweb object is currently open that your not using or did use and will be using later! Best to open the object, do your stuff and then finally dispose! try catch finally! or use the above to encapsulate your code within that example using statment!

as for the error, your trying to close the spweb object but somthing else is currently using it and so it throws the error! so having it in either a finally statment or the code above would do it when the code fully completes within the pipeline!

You need todo an iisreset and it should get rid of the spweb that is causing the issue and then you can try again! ;)

another example:



I am not sure how good this tool works. Its there on codeplex.

WinForms application that goes through ULS logs, parses them and detects log messages for undisposed objects.

It allows to:

  1. Scan for undisposed objects log entries and show full stack trace of undisposed SPRequest creation
  2. Exclude logs messages (to filter Microsoft dispose issues)
  3. Include only specific log messages

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