link icon
Ask The Community

TeamViewer is one of the winners of the European Customer Centricity Award 🥳

Please find more information here.

Delete objects with duplicate alias using last_seen attribute


Since we are including the Teamviewer agent in our OS-deployment we get duplicate alias when a computer is reinstalled.

I just wrote this script that removes duplicate objects with the same alias and only keeps the most recent object.

I am not the best at writing scripts so please test it and get back here if you think something should be changed.

It only deletes the outer loop object to keep the logic a little bit more simple.

Update with your token

If you want to test it without actually deleting objects you can comment out the following row to see what the end result would be:

Invoke-WebRequest -Uri "Https://$ID" -Method Delete -Headers $header

#script to identify duplicates alias in Teamviewer and deleting the objects with the oldest last seen timestamp

#define token
$bearer = "Bearer",$token

$header = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$header.Add("authorization", $bearer)

#Getting all the devices from API
$devices = (Invoke-RestMethod -Uri "Https://" -Method Get -Headers $header).devices

#Total number of devices
write-host "Total Number of devices" $devices.Count

#Looping through all devices
ForEach($device in $devices)
	#Settings variable for device ID
	$ID = $device.device_id
	#Setting device last seen variable
	$deviceLastseen = $device.last_seen
	#resetting device deleted
	$devicedeleted = $false
	#Looping through all devices again to be able to compare each object with all other objects
	ForEach($deviceloop in $devices)
		#Only do something if the device is not already delted
		if($devicedeleted -ne $true)
			#Setting inner loop variable for device last seen
			$deviceLastseenloop = $deviceloop.last_seen
			#Only do something if last seen values are populated
			if ($deviceLastseen -ne $null -and $deviceLastseenloop -ne $null)
				#Compare other loop device to inner loop device by alias, if they are the same but device ID is not, enter if statement.
				if($device.alias -eq $deviceloop.alias -and $device.device_id -ne $deviceloop.device_id )
					#cleanup date formatting
					$deviceLastseen = ($device.last_seen).Split("T")[0]
					$deviceLastseenloop = ($deviceloop.last_seen).Split("T")[0]
					Write-Host "Comparing "$device.alias $deviceLastseen $device.teamviewer_id "to" $deviceloop.alias "|" $deviceLastseenloop "|" $deviceloop.teamviewer_id -ForegroundColor Yellow
					#If duplicate found and outer loop device is older delete object
					if($deviceLastseen -lt $deviceLastseenloop)
						#Deleting outer loop device $ID = $device.device_id set above
						Invoke-WebRequest -Uri "Https://$ID" -Method Delete -Headers $header
						Write-Host "Deleted device:"$device.alias "|" $deviceLastseen "|" $device.teamviewer_id -ForegroundColor Green
						$devicedeleted = $true
			#reset last seen value
			$deviceLastseenloop = $null
	#reset last seen value
	$deviceLastseen = $null

#Total number of delted objects
write-host "Total number of deleted objects" $count
#Reset count
$count = $null

#read devices again
$devices = (Invoke-RestMethod -Uri "Https://" -Method Get -Headers $header).devices

#Total number of devices
write-host "Total Number of devices" $devices.Count

I take no responsibility for you using this script. Its on your own risk :)

Best regards



  • danieljoos
    danieljoos Posts: 6 Staff member 🤠

    Hi Dan,

    Thanks for posting this script.

    If you want, you can simplify this using our open-source TeamViewerPS Powershell module. It is possible to do all that in basically one line of Powershell 😉. However, I've split the line to make it better readable:

    # Install the module, if not already done
    Install-Module TeamViewerPS
    # Store the API token in the current Powershell session
    Get-TeamViewerDevice |   # Get all devices `
      Where-Object { $_.LastSeenAt } |   # Filter for devices with a 'last-seen' date `
      Group-Object -Property Name |      # Group them by device name `
      Where-Object {$_.Count -gt 1} |    # Filter for duplicates `
      ForEach-Object {
        $_.Group | `
        Sort-Object -Property LastSeenAt -Descending | # Sort the duplicate device entries by 'last-seen' (desc) `
        Select-Object -Skip 1 } |                      # Skip the first entry (newest) `
      Remove-TeamViewerDevice -Verbose                 # Remove the rest

    You can find more information about the TeamViewerPS Powershell module here:

    Best regards,


  • danisaksson
    danisaksson Posts: 1 Newbie

    Ok, thanks!

    I learned some stuff creating the script so it was not a total waste :)

    / Dan