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

Length

00:30:00

Difficulty

Beginner

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.

Create Storage Account and File Share

  1. Click on the Cloud Shell button (>_) located just to the right of the search bar.

  2. Select PowerShell.

  3. Leave the current subscription as is, and click Show advanced settings.

  4. When setting up your cloud shell, use the the same region as your Resource Group.

  5. Under File share, check Create new and name it "fileshareforcli".

  6. Click Create storage.

    Note: If it says these names are already taken, add the number 2 to the end of the Storage account and File share names.

  7. Once the Cloud Shell spins up, run the following to get the resource group name:

    Get-AzureRmResource

    Copy the ResourceGroupName from any of the resources. They all share the same resource group name.

Set a Variable in the PowerShell Command Line

  1. We'll set a variable here that gets the resource and resource group names:

    $r = Get-AzureRMResource -ResourceName lab-vm -ResourceGroupName <YOUR_COPIED_INFORMATION>

Apply Tags to the Virtual Machine

  1. Set our tags and connect it to our ID:

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

    A table appears with the status of Succeeded under the ProvisioningState section.

Verify Tags

  1. On the main page of our Azure Portal, click Virtual machines from the left-hand menu.
  2. On the Virtual machines page, selected our listed lab-VM.
  3. Click Tags. Here, we should see the tags we applied.

Conclusion

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