Syntax error How to Export the azure VM tags using PowerShell?

How to Export the azure VM tags using PowerShell?



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.

Example

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

Output

TagsProperty
------------
{[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.

Updated on: 2021-04-06T07:45:32+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements