How to Export the azure VM tags using PowerShell?

PowerShellMicrosoft TechnologiesSoftware & Coding

There are two ways to get the applied azure VM tags using PowerShell.

  • Using Tags Property of the Azure VM
  • Using the Get-AZTag command.


PS C:\> Get-AzVM -VMName Testmachine2k12 | Select -ExpandProperty Tags
Key          Value
---          -----
Patching_Day Sunday
Owner       Chirag

Another way is by using the Get-AZTag command.

PS C:\> $vm = Get-AzVM -VMName TestMachine2k12
PS C:\> Get-AzTag -ResourceId $vm.Id | Select -ExpandProperty Properties


{[Owner, Chirag], [Patching_Day, Sunday]}

We need to export this tag and the best way to store the tags is using the JSON file.

Get-AzVM -VMName Testmachine2k12 | Select -ExpandProperty Tags | ConvertTo-Json | Out-File C:\Temp\VMTag.json

You can see the tag file is stored in the C:\temp folder with the VMTag.json file name.

If you want to store the tags in the CSV file format then, we can use the below command.

Get-AzVM -VMName TestMachine2k16 | Select -ExpandProperty Tags | ConvertTo-Json | ConvertFrom-Json | Export-Csv C:\Temp\TestMachine2k16tags.csv -NoTypeInformation

The above command will store the azure VM TestMachine2k16 tags to the C:\Temp\TestMachine2k16tags.CSV format.

Published on 06-Apr-2021 11:14:54