# Is an object Disposed

**URL:** <https://forums.powershell.org/t/is-an-object-disposed/22559>\
**Category:** PowerShell Help\
**Created:** [July 5, 2023, 11:46am UTC](https://forums.powershell.org/t/is-an-object-disposed/22559 "2023-07-05T11:46:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lytvinh](https://avatars.discourse-cdn.com/v4/letter/l/3ec8ea/32.png) [@lytvinh](https://forums.powershell.org/u/lytvinh)\
**Post date:** [July 5, 2023, 11:46am UTC](https://forums.powershell.org/t/is-an-object-disposed/22559/1 "2023-07-05T11:46:26Z")

</div>

Is there a way to detect if an object is already disposed? An an example would be

```auto
$Watcher=New-Object -TypeName System.IO.FileSystemWatcher -Property @{
    Path="C:\"
    Filter="Test.txt"
    IncludeSubdirectories=$false
    NotifyFilter=[IO.NotifyFilters]::FileName, [IO.NotifyFilters]::LastWrite
}

```

I checked a few other forums and some came up with this:

```auto
$watcher -is [System.IDisposable]

```

But I believe that only detects if an object has the Dispose method. Does anyone have any ideas?

---

<div class="post-metadata">

**Author:** ![neemobeer](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/neemobeer/32/4087_2.png) [@neemobeer](https://forums.powershell.org/u/neemobeer)\
**Post date:** [July 6, 2023, 1:16pm UTC](https://forums.powershell.org/t/is-an-object-disposed/22559/2 "2023-07-06T13:16:08Z")

</div>

You would want to put a try/catch block around the code where an object may already be disposed. The catch type should be `catch [System.ObjectDisposedException] {...` then reinitialize the object for use.

---

<div class="post-metadata">

**Author:** ![lytvinh](https://avatars.discourse-cdn.com/v4/letter/l/3ec8ea/32.png) [@lytvinh](https://forums.powershell.org/u/lytvinh)\
**Post date:** [July 6, 2023, 2:11pm UTC](https://forums.powershell.org/t/is-an-object-disposed/22559/3 "2023-07-06T14:11:23Z")

</div>

When I dispose that object, I use:

```auto
$Watcher.Dispose()

```

I wouldn’t get an exception cause the Dispose method’s definition is:

```auto
void Dispose()
void IDisposable.Dispose()

```

I tried it to verify it was gonna work using this:

```auto
Try
{
    $Watcher.Dispose()
} 

Catch [System.ObjectDisposedException] {
    "An exception has been caught 1"
}

Catch {
    "An exception has been caught 2"
}

```

My console is blank and do not see any messages.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:14pm UTC](https://forums.powershell.org/t/is-an-object-disposed/22559/4 "2024-05-16T20:14:46Z")

</div>


