Skip to main content

Applying Tags to VMs in Azure Using PowerShell

Hands-On Lab


Photo of Chad Crowell

Chad Crowell

DevOps Training Architect II in Content





Tags are a great way to provide taxonomy to our resources in Azure. In this hands-on lab, we will be going through a scenario, in which a fictitious company has a problem that they need you to solve. Specifically, we will try to find the best way to help the billing department by tagging a virtual machine that has been costing them too much. In the pre-configured lab environment we’ve created, you will put your PowerShell skills to the test and add multiple tags to a lab virtual machine.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Applying Tags to Virtual Machines in Azure Using PowerShell

The Contoso Company’s billing department has asked us to apply some tags to their Virtual Machines (VMs) in order to simplify billing and help them understand where these costs are coming from on their billing report.

There was a storage account provisioned, but we need to create the fileshare in order to access PowerShell. To do this, we need to set up the appropriate fileshare and name it mycliforsettingtags, verify that we can access the PowerShell Command Line, and then use the appropriate PowerShell commands to apply the Environment and Department tags to the Virtual Machine.

Before we begin, we need to log in to our Azure Portal using the provided credentials.

Introduction to the Lab Environment

Once we are logged into the Azure Portal, we will find that we already have a directory, subscription, and resource group set up for our convenience. We will be using these throughout the lab. We also have a virtual machine already ready to go called lab-VM. This is the machine that we will be adding tags to.

Apply Tags to the VMs in Azure using PowerShell

Now that we're sitting in our environment, we can get started with attaching our tags, though we still have a few things to set up.

Set up the FileShare

To add tags, we first need to set up our fileshare. Let's click on the Cloud Shell button located just to the right of the search bar. It will look like >_ and opens up a new window at the bottom of the screen. Here, select PowerShell. Leave the current subscription as is.

Select Show advanced settings. Under Storage account check Create new and call it storageforcli. We will also create a new item for File share called filesharecli. Select Create Storage. It is possible that it will say these names are already taken. If so, add the number 2 to the end of the Storage account and File share names.

Now we need to get the resource group name. We can do that once PowerShell is up and running by entering the following command:


From any section, copy the ResourceGroupName, as it is the same for all of them.

Verify Access to the Powershell Command Line

Next we will set a variable, $r, with ResourceName and ResourceGroupName values. The latter is what we copied from the previous command's output. Use this command:

$r = Get-AzureRMResource -ResourceName lab-vm -ResourceGroupName xxxxxxxxx-applying_tags_to_vms_in_azure_using_powershell-xxx

Remember, these are all going be unique. Any x in the command is just a placeholder.

Apply Tags to the Virtual Machine

Now we can start adding our tags. We will be setting the tag IT to the name Dept, the tag Test to Environment, and will make sure that these are attached to our VM using the ResourceID fileshare we set up earlier. To set out tags and connect it to our ID, we will use the following command:

Set-AzureRmResource -Tag @{ Dept="IT"; Environment="Test" } -ResourceId $r.ResourceID -Force

A table appears, with the status of Succeeded under the ProvisioningState column.

Verify Tags

We can now close the command line and verify that these tags are on our virtual machine. On the main page of our Azure Portal, select Virtual Machines from the list on the left-hand side of the page. On the Virtual machines page, select our lab-VM machine and then select Tags.

Here we will see the tags that we applied.


Now that we've completed this lab, we know how to add tags to a virtual machine on Azure using PowerShell. Congratulations on completing the lab!