Hello All

Recently we had a request to setup or draft a process such that any active sync device partnership which is older than 30 days will be removed on nightly basis.

Now first thing that would come to mind is run powershell cmdlet manually on nightly basis while changing the date parameter in filter of cmdlet but as my name suggests, that’s not me.

So we put together a quick script which will look for all ActiveSync device partnerships and remove those which are older than 30 days. The script will also generate details of user & device partnership which it deleted based on different parameters. Once completed, we setup a bat file and put it in task scheduler to run on nightly basis and voila we’re all set.

You can also run the script in Report Only mode using -ReportOnly switch whcih allows you to only generate HTML report without removing the device partnerships from environment.

PowerShell
Edit|Remove
Function Remove-OldActiveSyncDevicePartnerhip ($DeviceInfo,$ReportOnly) {  
    $currentdate = Get-Date  
     foreach($device in $DeviceInfo) {   
        $stats = Get-ActiveSyncDeviceStatistics $device  
        $isdeviceold = ($currentDate - $stats.LastSuccessSync).Days  
        if ($isdeviceold -ge $NumberofDays -or $stats.LastSuccessSync -eq $null -and $stats.FirstSyncTime -ne $null) {  
            $mbx = Get-Mailbox $device.UserDisplayName  
            if ($mbx -eq $null) {  
                $username = $device.UserDisplayName  
                $emailadd = $null}  
            Else {  
                $username = $mbx.DisplayName  
                $emailadd = $mbx.WindowsEmailAddress}  
            $devicet = $stats.DeviceID  
            $devicem = $stats.DeviceModel  
            $devices = $stats.Status  
            $deviceas = $stats.DeviceAccessState  
            $firstsync = $stats.FirstSyncTime  
            $lastsync = $stats.LastSuccessSync  
            writedata $username $emailadd $devicet $devicem $devices $deviceas $firstsync $lastsync  
            If (!($ReportOnly)) {  
                Write-Host "Removing device partnerhip from environment"  
                Remove-ActiveSyncDevice $device -confirm:$false  
            }  
        }      
    }   
} 
 

 

Please download the file local to your machine and then rename to .PS1 from .txt before using. Based on your requirements, you can tweak the script and include more features as needed.

For detailed help along with parameters details & examples on how to use this script please run below cmdlet on Powershell

Get-Help .\Remove-OldActiveSyncDevicePartnership.ps1 -Detailed

Please let us know if any queries in attached script. Hope you find this helpful and it helps to keep your exchange environment clean & healthy

For any script ideas or requirements please post in comment section and we’ll try to get one for you. !

Thank You