How to get the details of the all Azure VM's with respective all Tag associated with it in excel sheet with the help of powershell?

surindersingh dhaliwal 81 Reputation points
2022-07-19T10:42:15.983+00:00

Is there any script or command to get the report of all azure VMs in an excel sheet with PowerShell's help, which includes the All tags associated with that, RG, and subscription?

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
7,108 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,360 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 44,776 Reputation points
    2022-07-20T14:48:20.447+00:00

    A tag is actually a hash. Each has a name and a value. You can't export them directly to a CSV file. Also, unless you're extremely careful, it's likely that you'll find a tag you didn't expect to find. That may because someone made a typo when creating the tag, or a tag was added that nobody remembers. So exporting all the tags for all VMs, with each VM having only one row in the CSV file requires you to have previous knowledge of ALL the tag names, or process all VMs and accumulate all the tag names so you can create an identical set of tag names for each row (whether or not the tag exists for all VMs).

    Here's an example of how to go about this: export-azure-resource-groups-with-tags-to-csv


2 additional answers

Sort by: Most helpful
  1. shiva patpi 13,131 Reputation points Microsoft Employee
    2022-07-20T06:00:19.857+00:00

    Hello @surindersingh dhaliwal ,
    Please use the below script:

    connect-AzAccount
    $VMResources = Get-AzVM #get all the VMs
    $vmdetails = New-Object System.Collections.ArrayList #variable to hold vm details in arraylist
    $singleVMDetails = [ordered]@{} #show the list in the order
    Write-Host "###########################"
    Write-Host "RGName, VMName, Tags" -ForegroundColor Red
    Write-Host "###########################" -ForegroundColor White
    foreach($VMResource in $VMResources) #loop through each VM
    {
    $existingTags = (Get-AzResource -ResourceGroupName $VMResource.ResourceGroupName -Name $VMResource.Name).Tags #get tags
    $singleVMDetails.'RGName' = $VMResource.ResourceGroupName
    $singleVMDetails.'VMName' = $VMResource.Name
    $singleVMDetails.'Tags' = $existingTags
    $vmdetails.Add((New-object PSObject -Property $singleVMDetails)) | Out-Null
    Write-Host $VMResource.ResourceGroupName "," $VMResource.Name "," $existingTags -ForegroundColor White #Display details on screen
    }
    $vmdetails | Export-Csv "vmdetails.csv" -NoTypeInformation -Encoding UTF8 -Delimiter ',' #Export to CSV

    output on the screen and it will create a vmdetails.csv file in the same directory with all the information;

    222539-image.png


  2. Limitless Technology 39,351 Reputation points
    2022-07-20T09:59:30.873+00:00

    Hi there,

    You can get the VM details by calling the tag.

    Function Get-VmDetailsByTag {
    Param (
    [string]$ResourceGroup,
    [hashtable]$Tag,
    [string]$Subscription,
    [string]$TenantID
    )

    Set-AzContext -Tenant $TenantID -Subscription $Subscription

    Get-AzResource -Tag $tag -ResourceType “Microsoft.Compute/virtualMachines”

    }

    The bellow article covers some of the Azure PowerShell commands that you can use to create and manage virtual machines in your Azure subscription. For more detailed help with specific command-line switches and options, you can use the Get-Help command.

    Common PowerShell commands for creating and managing Azure Virtual Machines

    https://learn.microsoft.com/en-us/azure/virtual-machines/windows/ps-common-ref

    --------------------------------------------------------------------------------------------------------------------------

    --If the reply is helpful, please Upvote and Accept it as an answer–

    0 comments No comments