How to tag Azure Resources using Powershell
Date published:
Tagging resources is a great way to organize your resources and make it easier to find them. You can also use tags to apply policies to resources. For example, you can use tags to apply a retention policy to all resources with a specific tag. This post will show how to use PowerShell to tag Azure Resources.
We will use the following resources to demonstrate how to tag resources using PowerShell.
- Virtual Machine
- resource group
We will tag all the resources in the resource group with the following tags.
- Department: IT
- Project: Azure Migration
- Owner: Nicholas Chang
- Log in to Azure using PowerShell on your local machine
- Connect-AzAccount
- Assign a variable for the resource group name
- $rg = Get-AzResourceGroup -Name tag-resources-powershell
- Next, set the tags for the resource group
- Set-AzResourceGroup -Name $rg.ResourceGroupName -tag @{ Department = “IT”; Project = “Azure Migration”; Owner = “Nicholas Chang” }
- Confirm the resource group and tags value. After, we will use it on all the resources within the resource group.
Get-AzResourceGroup -Name tag-resources-powershell | Select-Object ResourceGroupName, @{Name=“Tags”;Expression={$_.Tags}}
- Get-AZresource -ResourceGroupName $rg.ResourceGroupName | ForEach-Object {Set-AzResource -ResourceId $_.ResourceId -Tag $rg.Tags -force }
When running the command, all the resources will have the tag with the values we set in step 3.
I hope you enjoy this post. If you have any questions or comments, please leave them below.