Remove Devices in EXO using ForEach statements

Welcome Forums General PowerShell Q&A Remove Devices in EXO using ForEach statements

This topic contains 6 replies, has 3 voices, and was last updated by

1 year, 8 months ago.

  • Author
  • #62566


    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

  • #62574


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

  • #62577


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

  • #62578


    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 *
  • #62590


    Ron has a good point there.

    Check the help system, but also the help online:

    This works for me, for a single user:

    $easDevice = Get-MobileDevice -Mailbox | 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:

  • #62604


    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 !!!

  • #62832


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

The topic ‘Remove Devices in EXO using ForEach statements’ is closed to new replies.