Skip to main content
Jorge Bernhardt Jorge Bernhardt
  1. Posts/

Tag Azure Resources using Az PowerShell cmdlets

·613 words·3 mins· 100 views · 5 likes ·
Azure PowerShell Get-AzResource Get-AzSubscription Get-AzTag

This post, shows, how to use Azure PowerShell to add tags to an Azure Resource.

  • Tags are metadata that you can add to an Azure resource.
  • You can tag resources with name/value pairs to organize and group resources in your subscription.
  • Each resource or resource group can have a maximum of 15 tag name/value pairs.
Az PowerShell Workaround>

Az PowerShell Workaround #

If you want to know how to install the PowerShell Az module on your machine, check out this link. The simplest way to get started is to sign in interactively at the command line.

Login-AzAccount

If you have more than one subscription associated with your mail account, you can choose the default subscription. To perform this task we will use the following commands:

Get-AzSubscription
Select-AzSubscription -Subscription "My Subscription"

Once you set your default subscription, you are ready to add tags to your Azure Resources.

List all tags and their number of occurrences in your Azure subscription using PowerShell>

List all tags and their number of occurrences in your Azure subscription using PowerShell #

You can view all tags and their number of occurrences in your subscription using the Get-AzTag cmdlet. The Count property shows how many times the tag has been applied to resources and resource groups in your subscription.

Get-AzTag

Get-Aztag

Add tags to Azure Resources>

Add tags to Azure Resources #

To add a Tag to a Resource Group with Azure PowerShell, use the Set-AzResourceGroup cmdlet with the following syntax:

Set-AzResourceGroup `
    -Name RG-CLI `
    -Tag @{Owner=Jorge Bernhardt"}

Set-AzResourceGroup
To add a Tag to a Resource (Virtual Machines, Virtual Networks, etc ) with Azure PowerShell, use the Set-AzResourceGroup cmdlet with the following syntax:

$tag = @{Owner=Jorge Bernhardt"}

$VM = Get-AzResource `
    -ResourceGroupName RG-CLI `
    -Name VM-ARM-CLI

Set-AzResource `
    -ResourceId $vm.Id `
    -Tag $tag `
    -Force
Set-AzResource
>

Set-AzResource
#

Retrieve existing resource tags>

Retrieve existing resource tags #

If you want to obtain all tags applied to a Resource Group. you should use the Get-AzResourceGroup cmdlet with the following syntax:

(Get-AzResource -ResourceGroupName RG-CLI).Tags

Get-AzResourceGroup
If you want to obtain all tags applied to an Azure Resource. you should use the Get-AzResourceGroup cmdlet with the following syntax:

(Get-AzResource `
    -ResourceGroupName RG-CLI `
    -ResourceName VM-ARM-CLI).Tags
Get-AzResource
>

Get-AzResource
#

Add new tags to existing tags>

Add new tags to existing tags #

If you apply tags to a resource or resource group, overwrite existing tags in that resource or resource group. For this reason, we must use different methods to add tags to the resource or resource group that already has tags applied. To add a tag to a resource group that has existing tags, use :

$tags = (Get-AzResourceGroup -Name RG-CLI).Tags
$tags.Add("Project","www.jorgebernhardt.com")

Set-AzResourceGroup `
    -name RG-CLI `
    -Tag $tags

Set-AzResourceGroup
To add a tag to an Azure resource that has existing tags, use :

$tags = (Get-AzResource `
    -ResourceGroupName RG-CLI `
    -Name VM-ARM-CLI).Tags

$tags.Add("Project","www.jorgebernhardt.com")


Set-AzResource `
    -ResourceGroupName RG-CLI `
    -ResourceName VM-ARM-CLI `
    -ResourceType Microsoft.Compute/virtualMachines `
    -Tag $tags `
    -Force

You can also use the following method to add new tags to an Azure resource.

$tags = (Get-AzResource `
    -ResourceGroupName RG-CLI `
    -Name VM-ARM-CLI).Tags

$tags.Add("Project","www.jorgebernhardt.com")

$Resource = Get-AzResource `
    -Name VM-ARM-CLI

Set-AzResource `
    -ResourceId $resource.Id `
    -Tag $tags `
    -Force
Tags Azure PowerShell
>

Tags Azure PowerShell
#

Remove all tags>

Remove all tags #

To remove all the labels of an Azure resource, pass an empty hash table. Here we remove all tags from a resources group.

Set-AzResourceGroup `
    -Name RG-CLI `
    -Tag @{}

Tags Azure PowerShell
And here we remove all tags from an Azure resource

 $Resource = Get-AzResource `
    -Name VM-ARM-CLI

 Set-AzResource `
    -ResourceId $resource.Id `
    -Tag @{} `
    -Force

Tags Azure PowerShell

Thanks for reading my post. I hope you find it helpful.

If you want to know more about tags in Azure, check out this link.