How to tag a Windows virtual machine in Azure

This article describes different ways to tag a Windows virtual machine in Azure through the Resource Manager deployment model. Tags are user-defined key/value pairs which can be placed directly on a resource or a resource group. Azure currently supports up to 15 tags per resource and resource group. Tags may be placed on a resource at the time of creation or added to an existing resource. Please note that tags are supported for resources created via the Resource Manager deployment model only. If you want to tag a Linux virtual machine, see How to tag a Linux virtual machine in Azure.

Tagging a Virtual Machine through Templates

First, let’s look at tagging through templates. This template places tags on the following resources: Compute (Virtual Machine), Storage (Storage Account), and Network (Public IP Address, Virtual Network, and Network Interface). This template is for a Windows VM but can be adapted for Linux VMs.

Click the Deploy to Azure button from the template link. This will navigate to the Azure portal where you can deploy this template.

Simple deployment with Tags

This template includes the following tags: Department, Application, and Created By. You can add/edit these tags directly in the template if you would like different tag names.

Azure tags in a template

As you can see, the tags are defined as key/value pairs, separated by a colon (:). The tags must be defined in this format:

    “tags”: {
        “Key1” : ”Value1”,
        “Key2” : “Value2”

Save the template file after you finish editing it with the tags of your choice.

Next, in the Edit Parameters section, you can fill out the values for your tags.

Edit Tags in Azure portal

Click Create to deploy this template with your tag values.

Tagging through the Portal

After creating your resources with tags, you can view, add, and delete tags in the portal.

Select the tags icon to view your tags:

Tags icon in Azure portal

Add a new tag through the portal by defining your own Key/Value pair, and save it.

Add new Tag in Azure portal

Your new tag should now appear in the list of tags for your resource.

New Tag saved in Azure portal

Tagging with PowerShell

To create, add, and delete tags through PowerShell, you first need to set up your PowerShell environment with Azure Resource Manager. Once you have completed the setup, you can place tags on Compute, Network, and Storage resources at creation or after the resource is created via PowerShell. This article will concentrate on viewing/editing tags placed on Virtual Machines.

First, navigate to a Virtual Machine through the Get-AzureRmVM cmdlet.

    PS C:\> Get-AzureRmVM -ResourceGroupName "MyResourceGroup" -Name "MyTestVM"

If your Virtual Machine already contains tags, you will then see all the tags on your resource:

    Tags : {
            "Application": "MyApp1",
            "Created By": "MyName",
            "Department": "MyDepartment",
            "Environment": "Production"

If you would like to add tags through PowerShell, you can use the Set-AzureRmResource command. Note when updating tags through PowerShell, tags are updated as a whole. So if you are adding one tag to a resource that already has tags, you will need to include all the tags that you want to be placed on the resource. Below is an example of how to add additional tags to a resource through PowerShell Cmdlets.

This first cmdlet sets all of the tags placed on MyTestVM to the $tags variable, using the Get-AzureRmResource and Tags property.

    PS C:\> $tags = (Get-AzureRmResource -ResourceGroupName MyResourceGroup -Name MyTestVM).Tags

The second command displays the tags for the given variable.

    PS C:\> $tags

    Name        Value
    ----                           -----
    Value        MyDepartment
    Name        Department
    Value        MyApp1
    Name        Application
    Value        MyName
    Name        Created By
    Value        Production
    Name        Environment

The third command adds an additional tag to the $tags variable. Note the use of the += to append the new key/value pair to the $tags list.

    PS C:\> $tags += @{Name="Location";Value="MyLocation"}

The fourth command sets all of the tags defined in the $tags variable to the given resource. In this case, it is MyTestVM.

    PS C:\> Set-AzureRmResource -ResourceGroupName MyResourceGroup -Name MyTestVM -ResourceType "Microsoft.Compute/VirtualMachines" -Tag $tags

The fifth command displays all of the tags on the resource. As you can see, Location is now defined as a tag with MyLocation as the value.

    PS C:\> (Get-AzureRmResource -ResourceGroupName MyResourceGroup -Name MyTestVM).Tags

    Name        Value
    ----                           -----
    Value        MyDepartment
    Name        Department
    Value        MyApp1
    Name        Application
    Value        MyName
    Name        Created By
    Value        Production
    Name        Environment
    Value        MyLocation
    Name        Location

To learn more about tagging through PowerShell, check out the Azure Resource Cmdlets.

Viewing your tags in the usage details

Tags placed on Compute, Network, and Storage resources in the Resource Manager deployment model will be populated in your usage details in the billing portal.

Click on Download usage details to view the usage details in your subscription.

Usage details in Azure portal

Select your billing statement and the Version 2 usage details:

Version 2 Preview Usage Details in Azure portal

From the usage details, you can see all of the tags in the Tags column:

Tags column in Azure portal

By analyzing these tags along with usage, organizations will be able to gain new insights into their consumption data.

Next steps