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
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.
1 2 3 | 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:
1 2 3 4 | 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
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.
1 2 3 | Get-AzTag |
Add tags to an Azure Resources
To add a Tag to a Resource Group with Azure PowerShell, use the Set-AzResourceGroup cmdlet with the following syntax:
1 2 3 4 5 | Set-AzResourceGroup -Name RG-CLI ` -Tag @{Owner=“Jorge Bernhardt"} |
To add a Tag to a Resource (Virtual Machines, Virtual Networks, etc ) with Azure PowerShell, use the Set-AzResourceGroup cmdlet with the following syntax:
1 2 3 4 5 6 7 8 9 10 | $tag = @{Owner=“Jorge Bernhardt"} $VM = Get-AzResource -ResourceGroupName RG-CLI ` -Name VM-ARM-CLI Set-AzResource -ResourceId $vm.Id ` -Tag $tag ` -Force |
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:
1 2 3 | (Get-AzResource -ResourceGroupName RG-CLI).Tags |
If you want to obtain all tags applied to an Azure Resource. you should use the Get-AzResourceGroup cmdlet with the following syntax:
1 2 3 4 | (Get-AzResource -ResourceGroupName RG-CLI ` -ResourceName VM-ARM-CLI).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 :
1 2 3 4 5 6 7 | $tags = (Get-AzResourceGroup -Name RG-CLI).Tags $tags.Add("Project","www.jorgebernhardt.com") Set-AzResourceGroup -name RG-CLI ` -Tag $tags |
To add a tag to an Azure resource that has existing tags, use :
1 2 3 4 5 6 7 8 9 10 11 12 | $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.
1 2 3 4 5 6 7 8 9 10 | $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 |
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.
1 2 3 4 | Set-AzResourceGroup -Name RG-CLI ` -Tag @{} |
And here we remove all tags from an Azure resource
1 2 3 4 5 6 7 | $Resource = Get-AzResource -Name VM-ARM-CLI Set-AzResource -ResourceId $resource.Id ` -Tag @{} ` -Force |
If you want to know more about tags in Azure, check out this link: https://docs.microsoft.com/en-us/rest/api/resources/tags