# Remove Devices in EXO using ForEach statements

**URL:** <https://forums.powershell.org/t/remove-devices-in-exo-using-foreach-statements/7911>\
**Category:** PowerShell Help\
**Created:** [January 24, 2017, 9:39am UTC](https://forums.powershell.org/t/remove-devices-in-exo-using-foreach-statements/7911 "2017-01-24T09:39:30Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![powershell007](https://avatars.discourse-cdn.com/v4/letter/p/9e8a1a/32.png) [@powershell007](https://forums.powershell.org/u/powershell007)\
**Post date:** [January 24, 2017, 9:39am UTC](https://forums.powershell.org/t/remove-devices-in-exo-using-foreach-statements/7911/1 "2017-01-24T09:39:30Z")

</div>

I looking to remove activesync devices associated with a user.  
I’m having issues with the part2 “The operation couldn’t be performed because xxxxxxxxxxxx matches multiple entries.”

#import users  
$users = Get-Content -Path “C:\RemoveActiveSync.txt”

#Part 1:Remove allowed and blocked devices  
foreach ($user in $users){Set-CASMailbox -Identity $user -ActiveSyncAllowedDeviceIDs:$null; Set-CASMailbox -Identity $user -ActiveSyncblockedDeviceIDs:$null}

#Part 2:Remove the device partnership from the user.  
foreach ($user in $users){  
$EASDEVICES = Get-MobileDevice -Mailbox $user | select Identity  
foreach ($EASDEVICE in $EASDEVICES) {Remove-MobileDevice -Identity $EASDEVICE.identity -Confirm:$false  
}  
}

---

<div class="post-metadata">

**Author:** ![rdtechie](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rdtechie/32/273_2.png) [@rdtechie](https://forums.powershell.org/u/rdtechie)\
**Post date:** [January 24, 2017, 9:57am UTC](https://forums.powershell.org/t/remove-devices-in-exo-using-foreach-statements/7911/2 "2017-01-24T09:57:22Z")

</div>

Do you have a duplicate entry in your RemoveActiveSync.txt file?

---

<div class="post-metadata">

**Author:** ![powershell007](https://avatars.discourse-cdn.com/v4/letter/p/9e8a1a/32.png) [@powershell007](https://forums.powershell.org/u/powershell007)\
**Post date:** [January 24, 2017, 10:08am UTC](https://forums.powershell.org/t/remove-devices-in-exo-using-foreach-statements/7911/3 "2017-01-24T10:08:03Z")

</div>

No duplicate entries in the list.  
Can you tell me if the syntax looks correct for the nested foreach?

---

<div class="post-metadata">

**Author:** ![skalizar](https://avatars.discourse-cdn.com/v4/letter/s/ecc23a/32.png) [@skalizar](https://forums.powershell.org/u/skalizar)\
**Post date:** [January 24, 2017, 10:09am UTC](https://forums.powershell.org/t/remove-devices-in-exo-using-foreach-statements/7911/4 "2017-01-24T10:09:00Z")

</div>

I’m not familiar with the MobileDevice commands, but the online help doesn’t show an “Identity” parameter as output from Get-MobileDevice. If it is there, its clearly not a unique ID. You should run the command manually and output all of the fields. Pick an appropriate identity parameter that will be unique. My guess would be DeviceID.

```
Get-MobileDevice -Mailbox someuser | fl *
```

---

<div class="post-metadata">

**Author:** ![rdtechie](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rdtechie/32/273_2.png) [@rdtechie](https://forums.powershell.org/u/rdtechie)\
**Post date:** [January 24, 2017, 10:28am UTC](https://forums.powershell.org/t/remove-devices-in-exo-using-foreach-statements/7911/5 "2017-01-24T10:28:43Z")

</div>

Ron has a good point there.

Check the help system, but also the help online: [https://technet.microsoft.com/en-us/library/jj218674(v=exchg.160).aspx](https://technet.microsoft.com/en-us/library/jj218674(v=exchg.160).aspx)

This works for me, for a single user:

```
$easDevice = Get-MobileDevice -Mailbox demouser1@runas.io | select Identity
foreach ( $easDevice in $easDevices ) { Remove-MobileDevice -Identity $easDevice.Identity -Confirm:$false }
```

To verified the identity part, I checked the $easDevice variable. This containss the following array, which are correct entries for the -Identity parameter of Remove-MobileDevice:

```
Identity
--------
demouser1\ExchangeActiveSyncDevices\iPhone§Q57MITBC7145V72VMA0TUC6SO4
demouser1\ExchangeActiveSyncDevices\REST§Outlook§2f1f08887682f06855d2e97abf92b110
demouser1\ExchangeActiveSyncDevices\REST§Outlook§fad84ba321b2c5baedd00c387a99d9d6
demouser1\ExchangeActiveSyncDevices\iPhone§DJV96HE3PL4PDFG64A2I6Q6FVC
```

---

<div class="post-metadata">

**Author:** ![powershell007](https://avatars.discourse-cdn.com/v4/letter/p/9e8a1a/32.png) [@powershell007](https://forums.powershell.org/u/powershell007)\
**Post date:** [January 24, 2017, 2:45pm UTC](https://forums.powershell.org/t/remove-devices-in-exo-using-foreach-statements/7911/6 "2017-01-24T14:45:36Z")

</div>

This script is working. A few users are causing this error. “The operation couldn’t be performed because ‘xxxxxxx\ExchangeActiveSyncDevices\iPhone§3xxxxxxxxxxxxxxxxxxxx’ matches multiple entries.”

I going to pull them out of the list and manually try to remove the device.

foreach ($user in $users){Set-CASMailbox -Identity $user -ActiveSyncAllowedDeviceIDs:$null; Set-CASMailbox -Identity $user -ActiveSyncblockedDeviceIDs:$null}

foreach ($user in $users){  
$EASDEVICES = Get-MobileDevice -Mailbox $user | select Identity; foreach ($EASDEVICE in $EASDEVICES) {Remove-MobileDevice -Identity $EASDEVICE.identity -Confirm:$false}  
}

Thanks Richard for the tweak !!!

---

<div class="post-metadata">

**Author:** ![rdtechie](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rdtechie/32/273_2.png) [@rdtechie](https://forums.powershell.org/u/rdtechie)\
**Post date:** [January 27, 2017, 9:52am UTC](https://forums.powershell.org/t/remove-devices-in-exo-using-foreach-statements/7911/7 "2017-01-27T09:52:57Z")

</div>

No problem. I’m happy it worked for you. 🙂

---

<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:39pm UTC](https://forums.powershell.org/t/remove-devices-in-exo-using-foreach-statements/7911/8 "2024-05-16T20:39:22Z")

</div>


