Get azvm size using PowerShell | How to get VM size on AZURE?

This article guide to,

Get azvm size using PowerShell or How to get VM size on AZURE?

Many time we get requirement to Get all vms in subscription for audit purpose or to remove unwanted VM for cost saving purpose.

We will have many virtual machines in different Azure subscription for all environment like PROD, UAT, DEV, POC etc.

Below scripts can help you to Get all vms in subscription PowerShell or Get azure vm size PowerShell

You need to connect Azure Subscription before executing this script. Check below scripts as per your login configuration in Azure Subscription.

Connect Azure subscription to Get azure vm size PowerShell.

Note :If You have Microsoft Authenticator configured on mobile it will give you popup on mobile to approve for UseDeviceAuthentication method

$tenant = "tenantid" ##you can get it from portal or your administrator

$subscription = "Subscription Name" #you can get it from portal or your administrator

Connect-AzAccount -UseDeviceAuthentication -Tenant $tenant -SubscriptionName  $subscription

If you do not have these details use below script to connect subscription.

Connect-AzAccount -Tenant 'xxxx-xxxx-xxxx-xxxx' -SubscriptionId 'yyyy-yyyy-yyyy-yyyy'

or
Connect-AzAccount  #if you have only default subscription

In this script you can use Resource group name instead of location parameter if requirement to get Azure VM details from Resource group. Change path to export data in excel.

Get azvm size using PowerShell or How to get VM size on AZURE?

$vmSizes = Get-AzResourceGroup -Location "eastus" | Get-AzVM  | select Name,ResourceGroupName,location$vmSizes | Export-Csv -Path "C:\temp\AzureVm.csv" -NoTypeInformation

Get azvm size using PowerShell

$vmSizes = Get-AzResourceGroup -Location "eastus" | Get-AzVM  | select Name,ResourceGroupName,location

$vmSizes | Export-Csv -Path "C:\temp\AzureVm.csv" -NoTypeInformation # change path here

#Get VM size: You can add multiple locations in script to get all Azure VM details. Script may take time as per number of resources in Azure subscription per location.

$locations = Get-AzLocation | Where-Object {$_.location -in "eastus2","eastus"}

foreach ($l in $locations)

{

$vmSizes = Get-AzVmSize -Location $l.Location | Select-Object Name,NumberOfCores,MemoryInMB,MaxDataDiskCount,OSDiskSizeInMB,ResourceDiskSizeInMB

}
$vmSizes | Export-Csv -Path "C:\temp\AzureVmSizes.csv" -NoTypeInformation #Change Path here

Excel file will be exported on path mentioned in the script. This file will have VM sizes present on your Azure subscription.

Leave a Comment