2
2
title : Tag resources, resource groups, and subscriptions for logical organization
3
3
description : Shows how to apply tags to organize Azure resources for billing and managing.
4
4
ms.topic : conceptual
5
- ms.date : 03/18 /2020
5
+ ms.date : 03/19 /2020
6
6
---
7
7
# Use tags to organize your Azure resources, resource groups and subscriptions
8
8
@@ -28,7 +28,7 @@ The following example applies a set of tags to a storage account:
28
28
29
29
``` azurepowershell-interactive
30
30
$tags = @{"Dept"="Finance"; "Status"="Normal"}
31
- $resource = Get-AzResource -resourcename demoStorage -resourcegroup demoGroup
31
+ $resource = Get-AzResource -Name demoStorage -ResourceGroup demoGroup
32
32
New-AzTag -ResourceId $resource.id -Tag $tags
33
33
```
34
34
@@ -64,7 +64,7 @@ $tags = @{"Dept"="Finance"; "Status"="Normal"}
64
64
Update-AzTag -ResourceId $resource.id -Tag $tags -Operation Merge
65
65
```
66
66
67
- Notice all four tags have been applied to the resource .
67
+ Notice that the two new tags were added to the two existing tags .
68
68
69
69
``` output
70
70
Properties :
@@ -76,6 +76,23 @@ Properties :
76
76
Environment Production
77
77
```
78
78
79
+ If you provide a new value for a tag and use the merge operation, the old value is replaced. The following example changes the Status tag from Normal to Green.
80
+
81
+ ``` azurepowershell-interactive
82
+ $tags = @{"Status"="Green"}
83
+ Update-AzTag -ResourceId $resource.id -Tag $tags -Operation Merge
84
+ ```
85
+
86
+ ``` output
87
+ Properties :
88
+ Name Value
89
+ =========== ==========
90
+ Status Green
91
+ Dept Finance
92
+ Team Compliance
93
+ Environment Production
94
+ ```
95
+
79
96
When you set the ** -Operation** parameter to ** Replace** , the existing tags are replaced by the new set of tags.
80
97
81
98
``` azurepowershell-interactive
@@ -107,19 +124,46 @@ New-AzTag -ResourceId $resourceGroup.ResourceId -tag $tags
107
124
To update the tags for a resource group, use:
108
125
109
126
``` azurepowershell-interactive
110
- $tags = @{"Team "="Compliance "; "Environment"="Production"}
127
+ $tags = @{"CostCenter "="00123 "; "Environment"="Production"}
111
128
$resourceGroup = Get-AzResourceGroup -Name demoGroup
112
129
Update-AzTag -ResourceId $resourceGroup.ResourceId -Tag $tags -Operation Merge
113
130
```
114
131
115
- To add tags to a subscription, use:
132
+ ``` output
133
+ Properties :
134
+ Name Value
135
+ =========== ==========
136
+ CostCenter 00123
137
+ Environment Production
138
+ Status Normal
139
+ Dept Finance
140
+ ```
141
+
142
+ To add a new set of tags to a subscription, use:
116
143
117
144
``` azurepowershell-interactive
118
145
$tags = @{"CostCenter"="00123"; "Environment"="Dev"}
119
146
$subscription = (Get-AzSubscription -SubscriptionName "Example Subscription").Id
147
+ New-AzTag -ResourceId "/subscriptions/$subscription" -Tag $tags
148
+ ```
149
+
150
+ To update the tags for a subscription, use:
151
+
152
+ ``` azurepowershell-interactive
153
+ $tags = @{"Team"="Web Apps"}
154
+ $subscription = (Get-AzSubscription -SubscriptionName "Example Subscription").Id
120
155
Update-AzTag -ResourceId "/subscriptions/$subscription" -Tag $tags -Operation Merge
121
156
```
122
157
158
+ ``` output
159
+ Properties :
160
+ Name Value
161
+ =========== ========
162
+ Environment Dev
163
+ CostCenter 00123
164
+ Team Web Apps
165
+ ```
166
+
123
167
You may have more than one resource with the same name in a resource group. In that case, you can set each resource with the following commands:
124
168
125
169
``` azurepowershell-interactive
@@ -133,56 +177,52 @@ To apply tags from a subscription or resource group to the resources, see [Azure
133
177
134
178
### List tags
135
179
136
- To see the existing tags for a * resource group* , use:
137
-
138
- ``` azurepowershell-interactive
139
- (Get-AzResourceGroup -Name examplegroup).Tags
140
- ```
180
+ To get the tags for a resource, resource group, or subscription, use the [ Get-AzTag] ( /powershell/module/az.resources/get-aztag ) command and pass in the entity.
141
181
142
- That script returns the following format :
182
+ To see the tags for a resource, use :
143
183
144
- ``` output
145
- Name Value
146
- ---- -----
147
- Dept IT
148
- Environment Test
184
+ ``` azurepowershell-interactive
185
+ $resource = Get-AzResource -Name demoStorage -ResourceGroup demoGroup
186
+ Get-AzTag -ResourceId $resource.id
149
187
```
150
188
151
- To see the existing tags for a * resource that has a specified name and resource group* , use:
189
+ To see the tags for a resource group, use:
152
190
153
191
``` azurepowershell-interactive
154
- (Get-AzResource -ResourceName examplevnet -ResourceGroupName examplegroup).Tags
192
+ $resourceGroup = Get-AzResourceGroup -Name demoGroup
193
+ Get-AzTag -ResourceId $resourceGroup.ResourceId
155
194
```
156
195
157
- Or, if you have the resource ID for a resource, you can pass that resource ID to get the tags.
196
+ To see the tags for a subscription, use:
158
197
159
198
``` azurepowershell-interactive
160
- (Get-AzResource -ResourceId /subscriptions/<subscription-id>/resourceGroups/<rg-name>/providers/Microsoft.Storage/storageAccounts/<storage-name>).Tags
199
+ $subscription = (Get-AzSubscription -SubscriptionName "Example Subscription").Id
200
+ Get-AzTag -ResourceId "/subscriptions/$subscription"
161
201
```
162
202
163
203
### List resources by tag
164
204
165
- To get * resources that have a specific tag name and value* , use:
205
+ To get resources that have a specific tag name and value, use:
166
206
167
207
``` azurepowershell-interactive
168
- (Get-AzResource -Tag @{ "Dept "="Finance "}).Name
208
+ (Get-AzResource -Tag @{ "CostCenter "="00123 "}).Name
169
209
```
170
210
171
- To get * resources that have a specific tag name* , use:
211
+ To get resources that have a specific tag name with any tag value , use:
172
212
173
213
``` azurepowershell-interactive
174
214
(Get-AzResource -TagName "Dept").Name
175
215
```
176
216
177
- To get * resource groups that have a specific tag name and value* , use:
217
+ To get resource groups that have a specific tag name and value, use:
178
218
179
219
``` azurepowershell-interactive
180
- (Get-AzResourceGroup -Tag @{ "Dept "="Finance " }).ResourceGroupName
220
+ (Get-AzResourceGroup -Tag @{ "CostCenter "="00123 " }).ResourceGroupName
181
221
```
182
222
183
223
### Remove tags
184
224
185
- To remove specific tags, use ** Update-AzTag** and set ** -Operation** to ** Delete** .
225
+ To remove specific tags, use ** Update-AzTag** and set ** -Operation** to ** Delete** . Pass in the tags you want to delete.
186
226
187
227
``` azurepowershell-interactive
188
228
$removeTags = @{"Project"="ECommerce"; "Team"="Web"}
0 commit comments