How to change the Azure tag value using PowerShell?

PowerShellMicrosoft TechnologiesSoftware & Coding

To change the azure value using PowerShell we need to use the Update-AZTag command with the merge property.

Example

For example, we have the Azure VM TestMachine2k16 and we have its tags as shown below.

PS C:\> $vm = Get-AzVM -VMName TestMachine2k16
PS C:\> $vm | Select -ExpandProperty Tags

Output

Key          Value
---          -----
Owner        Chirag
For Ansible
Patching_Day Sunday
Application SecretTag

We need to change the Patching_Day from Sunday to Wednesday. We will use the below command.

Example

$tag = @{Patching_Day='Wednesday'}
Update-AzTag -Tag $tag -ResourceId $vm.Id -Operation Merge -Verbose

Output

Name           Value
============   =========
Patching_Day   Wednesday
For            Ansible
Application    SecretTag
Owner          Chirag

There is also property Replace available in Update-AZTag but please be cautious while using the Replace parameter because once you apply it, the current tag will be changed but other tags will be erased.

Example

Update-AzTag -Tag $tag -ResourceId $vm.Id -Operation Replace -Verbose

Output

Properties :
Name          Value
============  =========
Patching_Day  Wednesday
raja
Published on 06-Apr-2021 11:28:29
Advertisements