How to tag your IoT Hub Devices

Until now, device twin tags were a bit lame.

Yes, desired and reported properties were much more fun to play with.

But those of you who administer thousands of Azure IoT devices, you really appreciate tags. It’s the only way to control that large amount of devices without losing your head.

Why? Because you first query your devices and then execute jobs on these subsets.

And Microsoft is making use of this feature a lot. You will have to use tags if you want to execute IoT Edge deployments (still in preview) or if you want to use the recently added Automatic Device Management (even newer):

But how do you actually add or alter tags of devices? What tooling is Microsoft providing?

Let’s check out a number of ways to tag and start querying your devices.

Tags in the Azure Portal

If you are brave, you can enter tags in the Azure Portal already.

Just create an IoT device and open the Device Twin. At the bottom of the twin, we add this single tag:

Save this twin and see how it is accepted (and see how the twin is rearranged, somehow there is a specific order of the properties).

I feel a bit insecure, editing the actual JSON body.

Let’s try out another tool.

Tags in the Device Explorer

The device Explorer supports altering tags too!

We can check out the Twin props:

Note: This button is only available in the more recent versions of the Device Explorer.

So let’s alter the current tag and add one extra:

Push that button and see how the changes are merged:

This is a nice feature of the Device Explorer but it’s a typical Windows application. Can we do this with other tools too?

Tags in Visual Studio Code

In Visual Studio Code, the Azure IoT Toolkit supports alerting the device twin too:

The Device Twin is shown as a file in the editor. Now alter the tags and right click the file:

In the Output windows, you can see the twin is actually updated:

Again, we have seen a tool which is very useful. How about programming some code?

Tags in Visual Studio

Now let’s alter the tags using coding in Visual Studio.

Tags are only available server-side so we need the Nuget package named “Microsoft.Azure.Devices”.

So start a Console app application:

using Microsoft.Azure.Devices;
using Microsoft.Azure.Devices.Shared;
using System;

namespace ConsoleAppServerSide
  class Program
    static RegistryManager registryManager;
    static string targetDevice = "TestDevice";
    static string connectionString = "[IoT Hub owner connection string]";

    static void Main(string[] args)
      registryManager = RegistryManager.CreateFromConnectionString(connectionString);

      Twin twin = registryManager.GetTwinAsync(targetDevice).GetAwaiter().GetResult();


      registryManager.UpdateTwinAsync(targetDevice, "{\"tags\": {\"sourceTag\":\"Visual Studio\"}}", twin.ETag).GetAwaiter().GetResult();

      Twin twin2 = registryManager.GetTwinAsync(targetDevice).GetAwaiter().GetResult();


      var query = registryManager.CreateQuery("SELECT * FROM devices WHERE tags.sourceTag='Visual Studio'", 5);

      var i = 0;

      while (query.HasMoreResults)
        var page = query.GetNextAsTwinAsync().GetAwaiter().GetResult();

        foreach(var item in page)
          Console.WriteLine($"Device found: '{item.DeviceId}'");

      Console.WriteLine($"The query resulted in {i} devices.");
      Console.WriteLine("Press a key to exit");

When we execute this code, We get this result:

First, we get the device twin of the IoT Device. We then read the tags. Next, we alter that same device tags and print them afterward.

Next, we query for that tag and we find our device.

The query language is quite powerful. Please check this for more details.


Microsoft is very busy to bring the Azure IoT tooling on par with every tool they provide. We have seen many ways to change the tags, just pick your own favorite way.