How to Get Azure VM License Type?

To get the licenses for Azure Virtual Machines using Azure PowerShell, you can use the following Azure PowerShell script to retrieves information about one or more virtual machines in an Azure subscription and to export the Virtual Machines Licenses details to a CSV file.

Note: Please make sure you have the required permissions to access the subscription and the virtual machines.

Azure PowerShell Script:

$Data = @()
$Subscription = "_add_subscription_name_"
Set-AzureContext -SubscriptionName "$Subscription"
$ResourceGroups = Get-AzureResourceGroup

foreach ($RG in $ResourceGroups) {
    $VMs = Get-AzureVM -ResourceGroupName $RG.ResourceGroupName
    foreach($VM in $VMs) {

        if (!$VM.LicenseType) {
            $LicenseType = "No_License"
        }
        else {
            $LicenseType = $VM.LicenseType 
        }
    
        $VMCustom = New-Object System.Object
        $VMCustom | Add-Member -Type NoteProperty -Name VMName -Value $VM.Name
        $VMCustom | Add-Member -Type NoteProperty -Name Subscription -Value $Subscription
        $VMCustom | Add-Member -Type NoteProperty -Name RGNAME -Value $VM.ResourceGroupName
        $VMCustom | Add-Member -Type NoteProperty -Name Location -Value $VM.Location
        $VMCustom | Add-Member -Type NoteProperty -Name OSType -Value $VM.StorageProfile.OSDisk.OSType
        $VMCustom | Add-Member -Type NoteProperty -Name LicenseType -Value $LicenseType

        $VMCustom
        $Data += $VMCustom
    } 
}
$Data | Export-CSV "./VMlicenceDetails.csv" -Delimiter ";" -NoTypeInformation