Delete a group from your Computers and Contacts using API - TeamViewer Support
<main>
<article class="userContent">
<p>TeamViewer provides a web-based API that allows you to access data and control various aspects of your TeamViewer account. You can use the API to develop apps that integrate TeamViewer functionality into your corporate environment or develop apps that everyone can use.</p><div class="blockquote"><div class="blockquote-content"><p class="blockquote-line"><em>This article applies to all users. </em></p></div></div><p><br></p><h3></h3><h2 data-id="introduction">Introduction</h2><p>Imagine that you need to remove a group with devices inside, one by one, and then delete a group. That’s OK when you have a couple of devices, but when you have hundreds may be a hassle. Let’s take advantage of the API technology by creating a script that will automatically delete a group for you, including devices inside of it. All you need to do provide the API token and the group name. This article will show you how.</p><p>From a detailed perspective, we will be using the Group Management and Device Management API functions. Windows PowerShell was used to implement the solution. Using a user token, the code will retrieve the devices and groups from your management console, display them and let you choose what group you want to delete. After making the selection, the result will be a neat console.</p><p><br></p><h3 data-id="-1"></h3><h2 data-id="how-to-get-started">How to get started</h2><p>Make sure you have a TeamViewer account. If you don’t have one, you may get one free at <a href="http://login.teamviewer.com" rel="nofollow noreferrer ugc">http://login.teamviewer.com</a></p><p>To create a script, please log in to the TeamViewer Management Console with your TeamViewer account and create a script token.</p><p>In the Console, open your profile settings in the top right of the website. Then select Apps and click Create script token. Complete the form to define your token</p><p>• Enter your script name</p><p>• Enter a description for your script token</p><p>• Choose the permissions of your script token</p><p>• Group Management – View, create, delete edit, and share groups</p><p>• Computers & Contacts – View, add, edit and delete entries</p><p>• Save the script token</p><p>Now, save the token in a safe place. This will be requested by the script when it’s running.</p><p><strong>The Code</strong></p><pre class="code codeBlock" spellcheck="false" tabindex="0">$token = Read-Host -Prompt "Paste your account token code here"
$bearer = "Bearer",$token
$header = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$header.Add("authorization", $bearer)
$webrequest = Invoke-RestMethod -Uri "https://webapi.teamviewer.com/api/v1/groups/" -Method Get -Headers $header
$machine = Invoke-RestMethod -Uri "Https://webapi.teamviewer.com/api/v1/devices/" -Method Get -Headers $header
$i=1
$grpArr = @()
ForEach($grp in $webrequest.groups)
{
Write-Host $i ")" $grp.name " - " $grp.id
$grpArr += $grp.id;
ForEach($dev in $machine.devices)
{
If($dev.groupid -eq $grp.id)
{
Write-Host " " $dev.alias
}
}
$i += 1
}
$ig = Read-Host -Prompt "Select the group you will like to remove"
ForEach ($dgrp in $machine.devices)
{
if($dgrp.groupid -eq $grpArr[$ig - 1])
{
Write-Host "Delete device: " $dgrp.alias
$item = $dgrp.device_id
$delete = Invoke-WebRequest -Uri "Https://webapi.teamviewer.com/api/v1/devices/$item" -Method Delete -Headers $header
}
}
Write-Host "Delete group id: " $grpArr[$ig - 1]
$gid = $grpArr[$ig - 1]
$remove = Invoke-WebRequest -Uri "Https://webapi.teamviewer.com/api/v1/groups/$gid" -Method Delete -Headers $header
</pre><p> </p><h3 data-id="-2"></h3><h2 data-id="how-to-run-the-script"><strong>How to run the script</strong></h2><p>Copy the script code into your favorite text editor (ex. Notepad), and save as .ps1 file. (test.ps1)</p><p><br></p><h3 data-id="-3"></h3><h2 data-id="video"><strong>Video</strong></h2><p>Please watch the following video for detailed information on how the script works.</p><div class="js-embed embedResponsive" data-embedjson="{"height":113,"width":200,"photoUrl":"https:\/\/i.ytimg.com\/vi\/GQ50KliqGcQ\/hqdefault.jpg","videoID":"GQ50KliqGcQ","showRelated":false,"start":0,"url":"https:\/\/www.youtube.com\/watch?v=GQ50KliqGcQ","embedType":"youtube","name":"Group delete PS Script for TeamViewer accounts","frameSrc":"https:\/\/www.youtube.com\/embed\/GQ50KliqGcQ?feature=oembed&autoplay=1"}">
<a href="https://www.youtube.com/watch?v=GQ50KliqGcQ" rel="nofollow noreferrer ugc">
https://www.youtube.com/watch?v=GQ50KliqGcQ
</a>
</div><p><br></p><h3 data-id="-4"></h3><h2 data-id="the-bottom-line"><strong>The Bottom Line</strong></h2><p>TeamViewer API is valuable when you need to get the most from your management console. By combining this with a programming language and your imagination, you might get instant results like this for your everyday tasks. Enjoy!</p>
</article>
</main>