# Powershell Mutex not working as expected

**URL:** <https://forums.powershell.org/t/powershell-mutex-not-working-as-expected/7213>\
**Category:** PowerShell Help\
**Created:** [September 19, 2016, 2:43pm UTC](https://forums.powershell.org/t/powershell-mutex-not-working-as-expected/7213 "2016-09-19T14:43:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![toledotown](https://avatars.discourse-cdn.com/v4/letter/t/ecd19e/32.png) [@toledotown](https://forums.powershell.org/u/toledotown)\
**Post date:** [September 19, 2016, 2:43pm UTC](https://forums.powershell.org/t/powershell-mutex-not-working-as-expected/7213/1 "2016-09-19T14:43:07Z")

</div>

Hello all,

I am attempting to work with mutexes in Powershell for inter-process synchronization.

Say I open one PS ISE and perform the below:

```
$mutex = New-Object -TypeName system.threading.mutex($true, "TestMutex")
```

Then if I open a new ISE tab or a new console session and issue the below:

```
$mutex = New-Object -TypeName system.threading.mutex($true, "TestMutex")
```

No error is thrown. I would expect an error since a mutex by this name should already exist. Furthermore, the below does not return the mutex like I would expect (when run on the new console):

```
[System.Threading.Mutex]::OpenExisting("TestMutex")
```

And finally, if I performed the below, I would expect a return code of “False” as the mutex should be busy. But I always get “True” (again, on the new console session):

```
$mutex = New-Object -TypeName system.threading.mutex($false, "TestMutex")
if ($mutex.WaitOne(5000)) { write-output "free" }
else {write-output "mutex busy"}
```

Any help would be appreciated.

---

<div class="post-metadata">

**Author:** ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)\
**Post date:** [September 19, 2016, 2:52pm UTC](https://forums.powershell.org/t/powershell-mutex-not-working-as-expected/7213/2 "2016-09-19T14:52:24Z")

</div>

We’ll see if anyone pops in with an answer, but you’re a bit outside the PowerShell-verse, here. StackOverflow might be a better spot to ask. Unfortunately, I know practically nothing about .NET’s implementation of mutexes to tell you if this is a problem related to PowerShell being essentially single-threaded, with its scope, the fact that PowerShell runs in the DLR, or something else entirely.

---

<div class="post-metadata">

**Author:** ![sldrman](https://avatars.discourse-cdn.com/v4/letter/s/cab0a1/32.png) [@sldrman](https://forums.powershell.org/u/sldrman)\
**Post date:** [May 9, 2017, 12:19pm UTC](https://forums.powershell.org/t/powershell-mutex-not-working-as-expected/7213/3 "2017-05-09T12:19:30Z")

</div>

Use this to determine if you get mutex:

```
$CreatedMutex = ""
$Mutex = New-Object -TypeName system.threading.mutex($true, "TestMutex", [ref] $CreatedMutex)
if (!$CreatedMutex) {
    $Mutex.WaitOne()
}
# Do Stuff
$Mutex.ReleaseMutex()
$Mutex.Close()
```

You also might try “Global\TestMutex” as the name to make sure it is a global mutex. You can read more about it at [https://msdn.microsoft.com/en-us/library/bwe34f1k.aspx](https://msdn.microsoft.com/en-us/library/bwe34f1k.aspx)

Timed out example:

```
$CreatedMutex = ""
$Mutex = New-Object -TypeName system.threading.mutex($true, "Global\TestMutex", [ref] $CreatedMutex)
if (!$CreatedMutex) {
    if (!$Mutex.WaitOne(5000)) {
        throw "Timed out waiting for TestMutex"
    }
}
# Do Stuff
$Mutex.ReleaseMutex()
$Mutex.Close()
```

Hope this helps…  
SLDR  
(Stephen L. De Rudder)

Note: It does look like that mutexes are always created as session level mutexes even when Global\TestMutex is used. So powershells that are on different sessions will not work. I don’t know if PowerShell run as admin will allow truly global mutexes to be created.

---

<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:38pm UTC](https://forums.powershell.org/t/powershell-mutex-not-working-as-expected/7213/4 "2024-05-16T20:38:47Z")

</div>


