This article will provide a PowerShell script to export the Azure Resource Tags for each resource that is in your Azure Subscriptions.
This data is very helpful to help research resources that do not have any tags, or if specific tags are missing that are intended to be on every resource.
Tagging Azure resources is strongly recommended to help group resources together. One example of using tagging is within Azure Cost Management to see the costs of an application name, department, or cost center.
# This script will create an output CSV file here: "C:\temp\azure-resources.csv" and the folder "C:\Temp"
# if it does not exist
#
$Path="C:\Temp\"
# Change the path if needed to be another directory. End with the slash \
#
#
if (!(Test-Path $Path))
{
New-Item -itemType Directory -Path $Path
}
#
Connect-AzAccount
$getallSubscriptions = Get-AzSubscription
foreach($subscription in $getallSubscriptions)
{
Select-AzSubscription $subscription
$rgs = Get-AzResource #Group
foreach($rg in $rgs)
{
$TagsAsString = ""
$Tags = $rg.Tags
if($Tags -ne $null)
{
foreach($Tag in $Tags)
{
$TagsAsString += $Tag.Keys + ":" + $Tag.Values + ";"
}
}
$value = $($subscription.Name) + "," + $($rg.ResourceName) + "," + $($rg.ResourceGroupName) + "," + $rg.Location + ", Tags:" + $TagsAsString.Replace(":", "=")
Add-content -path "$($Path)azuretags.csv" -value $value
}
}
If you are only looking for resources in the current subscription that do not have any tags this is a quick example to run.
# Below example will create a CSV that only shows resources that do not have any tags.
Get-AzResource | Where-Object Tags -eq $null | Select-Object -Property Name, ResourceType, ResourceGroupName, location |Export-csv "tags.csv"
The link below gives examples of tags to consider if you are just starting to build out your tagging strategy.
Define your tagging strategy – Cloud Adoption Framework | Microsoft Docs