none
Remove Stale/Old Active Sync Devices from All Mailboxes

    Question

  • I have a script that goes out and checks each mailbox and gets the DeviceType, DevicePolicyApplied, LastSuccessSync, DeviceUserAgent, and the User for each mailbox on Exchange 2010 that has an Active Sync device.  You can find it here:  http://blog.powershell.no/2010/09/26/getting-an-overview-of-all-activesync-devices-in-the-exchange-organization

    Looking through that CSV file results, I can see A LOT of cleanup that needs to be done. I know users can manage their own devices through Outlook Web, but I can clearly see that is not happening. Some are even getting close to the 10 device limit.

    Is there a way, other than going to each mailbox in the management console, to remove stale/old active sync device partnerships that haven't sync'd in a given period of time. 6 months? a year?

    I would like to get an accurate read out of the types of devices people are using, but with so many old devices out there, making it hard to do so.

    Also, wondering if it's good practice to clean up stale active sync device partnerships. Would be nice if Exchange had a built in automatic way to handle these stale devices.


    • Edited by LE2Strat Thursday, April 26, 2012 1:16 PM
    Thursday, April 26, 2012 1:14 PM

Answers

  • This will get all devices that haven't tried to sync in the past 30 days:

    Get-ActiveSyncDevice -ResultSize unlimited | Get-ActiveSyncDeviceStatistics | where {$_.LastSyncAttemptTime -lt (get-date).adddays(-30)}


    You could pipe the results of this to:

    Remove-ActiveSyncDevice

    UPDATE (Jan 2013):  Remove-ActivesyncDevice now seems to fail with an error "can't find recipient..."

    Pipe to this instead: 

    foreach-object {Remove-ActiveSyncDevice ([string]$_.Guid) -confirm:$false}

    e.g.:

    $DevicesToRemove = Get-ActiveSyncDevice -result unlimited | Get-ActiveSyncDeviceStatistics | where {$_.LastSuccessSync -le (Get-Date).AddDays("-30")}
    
    $DevicesToRemove | foreach-object {Remove-ActiveSyncDevice ([string]$_.Guid) -confirm:$false}



    Mike Crowley | MVP
    My Blog -- Planet Technologies





    Thursday, April 26, 2012 8:42 PM
    Moderator

All replies

  • This will get all devices that haven't tried to sync in the past 30 days:

    Get-ActiveSyncDevice -ResultSize unlimited | Get-ActiveSyncDeviceStatistics | where {$_.LastSyncAttemptTime -lt (get-date).adddays(-30)}


    You could pipe the results of this to:

    Remove-ActiveSyncDevice

    UPDATE (Jan 2013):  Remove-ActivesyncDevice now seems to fail with an error "can't find recipient..."

    Pipe to this instead: 

    foreach-object {Remove-ActiveSyncDevice ([string]$_.Guid) -confirm:$false}

    e.g.:

    $DevicesToRemove = Get-ActiveSyncDevice -result unlimited | Get-ActiveSyncDeviceStatistics | where {$_.LastSuccessSync -le (Get-Date).AddDays("-30")}
    
    $DevicesToRemove | foreach-object {Remove-ActiveSyncDevice ([string]$_.Guid) -confirm:$false}



    Mike Crowley | MVP
    My Blog -- Planet Technologies





    Thursday, April 26, 2012 8:42 PM
    Moderator
  • Any update?  Did this work for you?


    Mike Crowley | MVP
    My Blog -- Planet Technologies

    Monday, April 30, 2012 6:13 PM
    Moderator
  • Yes that worked.
    Monday, April 30, 2012 6:17 PM
  • I tried your suggestion but get a lot of error stating it can't find "domain.com/users/xxxx" as a recipient ??? any idea?
    Thursday, September 27, 2012 6:59 PM
  • I tried your suggestion but get a lot of error stating it can't find "domain.com/users/xxxx" as a recipient ??? any idea?
    See the updated answer above


    Mike Crowley | MVP
    My Blog -- Planet Technologies

    Thursday, January 10, 2013 7:47 PM
    Moderator
  • Worked Great! Thank you!
    Thursday, February 14, 2013 7:41 PM
  • Fantastic !!! Thank you!!!

    cd..

    Friday, March 08, 2013 7:41 PM
  • This is great however I have a slightly more complex issue.  We did a little AD restructuring and users were moved from OU to OU and now we are trying to get MDM in the mix. 
    There are errors due to the MS bug with EAS trying remove the device from EAS however I cannot simply move the user back to the original OU because the last name has changed as well.

    Any thoughts on how to get around this?

    Thanks

    Monday, July 01, 2013 2:22 PM
  • Hi Mike:

       Thanks for the script. I'm using the second version of your script, and am seeing a lot of red in the powershell output. The same as the other person on this thread.

       The majority of this red is coming from disabled users who have mailboxes that are waiting for archiving. The mailboxes are hidden, but the AD accounts are disabled.

        Would this affect the ability to remove the active sync association?

    Thanks.

    Friday, November 07, 2014 7:56 PM
  • Pipeline not run because a pipeline is already running. Pipelines cannot be run concurrently.
        + CategoryInfo          : OperationStopped: (Microsoft.Power...tHelperRunspace:ExecutionCmdletHelperRunspace) [],
       PSInvalidOperationException
        + FullyQualifiedErrorId : RemotePipelineExecutionFailed

    This is the error I receive when I try running:

    Get-ActiveSyncDevice -ResultSize unlimited | Get-ActiveSyncDeviceStatistics | where {$_.LastSyncAttemptTime -lt (get-date).adddays(-30)} | foreach-object {Remove-ActiveSyncDevice ([string]$_.Guid) -confirm:$true}

    This is also on Exchange 2013

    Thoughts?

    Tuesday, April 07, 2015 9:21 PM
  • Forgot to set it as a variable
    Tuesday, April 07, 2015 9:41 PM
  • This is great however I have a slightly more complex issue.  We did a little AD restructuring and users were moved from OU to OU and now we are trying to get MDM in the mix. 
    There are errors due to the MS bug with EAS trying remove the device from EAS however I cannot simply move the user back to the original OU because the last name has changed as well.

    Any thoughts on how to get around this?

    Thanks

    The updated script above uses a GUID, so it is no longer dependent on the user's location in AD.  Are you saying that the GUIDs aren't the same anymore, due to your domain restructuring?  I don't have an answer for that scenario without better understanding the nature of the restructuring.  Perhaps just leave them alone, since they aren't really hurting anything anyway.  :)



    Mike Crowley | MVP
    My Blog -- Baseline Technologies

    Friday, April 10, 2015 9:28 PM
    Moderator
  • What variable is needed to be set? 

    Monday, February 01, 2016 7:41 PM
  • $DevicesToRemove

    Mike Crowley | MVP
    My Blog -- Baseline Technologies

    Monday, February 01, 2016 8:05 PM
    Moderator
  • Thanks Mike, this worked great!

    I wonder, is there a command that can be put in the script to output to a csv or text file the devices that were removed?

    Thanks again!

    Monday, April 04, 2016 2:21 PM
  • Get-ActiveSyncDevice -result unlimited | Get-ActiveSyncDeviceStatistics | where {$_.LastSuccessSync -le (Get-Date).AddDays("-30")} | export-csv filename.csv
    


    Thursday, April 07, 2016 10:13 AM
  • This will get all devices that haven't tried to sync in the past 30 days:

    Get-ActiveSyncDevice -ResultSize unlimited | Get-ActiveSyncDeviceStatistics | where {$_.LastSyncAttemptTime -lt (get-date).adddays(-30)}


    You could pipe the results of this to:

    Remove-ActiveSyncDevice

    UPDATE (Jan 2013):  Remove-ActivesyncDevice now seems to fail with an error "can't find recipient..."

    Pipe to this instead: 

    foreach-object {Remove-ActiveSyncDevice ([string]$_.Guid) -confirm:$false}

    e.g.:

    $DevicesToRemove = Get-ActiveSyncDevice -result unlimited | Get-ActiveSyncDeviceStatistics | where {$_.LastSuccessSync -le (Get-Date).AddDays("-30")}
    
    $DevicesToRemove | foreach-object {Remove-ActiveSyncDevice ([string]$_.Guid) -confirm:$false}



    Mike Crowley | MVP
    My Blog -- Planet Technologies





    This no longer works, as of 13/10/16

    Can we have a working solution please?

    Thanks.

    • Edited by Squuiid Thursday, October 13, 2016 11:38 AM
    Thursday, October 13, 2016 11:37 AM
  • First Run Below Command to get the devices with

    D:\>Get-ActiveSyncDevice –ResultSize Unlimited | Get-ActiveSyncDeviceStatistics | Select-Object DeviceID, Identity, LastSuccessSync, Guid | Export-CSV D:\DevicesSyncing.csv 

    Filter the Excel with LastSucessSync time DAYS

    Copy “Guid” of the users which want to remove (Conditions Like last contacted 90 days)> in one notepad 2000 users at a time .

    Name that notepad > “StaleRecord.txt

    Verify Guid table is having output ..

    Once this is ready

    D:\>Get-Content D:\StaleRecord.txt | Remove-ActiveSyncDevice

    Once shell command is successfully

    Update Next 2000 users GUID in notepad > Save

    Run This command : Get-Content “D:\StaleRecord.txt” | Remove-ActiveSyncDevice

    Wednesday, October 26, 2016 2:05 PM