Powershell script to get list of all VMs in all subscriptions

Bhavishka Sathawane 26 Reputation points
2022-09-03T19:24:33.557+00:00

I've got many subscriptions in my tenant ID say sub 1 sub 2 sub 3 sub4 and sub5.
I wanted to get list of all vms in all subscriptions except for one subscription say sub3 .

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
7,129 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. shiva patpi 13,141 Reputation points Microsoft Employee
    2022-09-04T03:35:04.4+00:00

    Hello @Bhavishka Sathawane ,
    Please use below powershell script, read out the comments for each line so that you can understand :

    //Get All Subscriptions
    $subs = Get-AzureRmSubscription
    //Arry to store list of VMs
    $vmobjs = @()
    //loop through each subscription
    foreach ($sub in $subs)
    {
    //Display the current processing subscription
    Write-Host "Processing subscription $($sub.Name)"
    try
    {
    //Select the subscription
    //please add the condition if you want to skip a particular subscription
    Select-AzureRmSubscription -SubscriptionId $sub.SubscriptionId -ErrorAction Continue
    //Get all the VMs information
    $vms = Get-AzureRmVm
    //loop through all the VMs
    foreach ($vm in $vms)
    {
    write-host $vm.Name , $vm.ResourceGroupName , $vm.HardwareProfile.VmSize , $vm.OsType , $vm.ProvisioningState , $vm.Location , $vm.StorageProfile.OsDisk.Name
    $VMReport += New-Object psobject -Property @{
    "SubName" = $sub.Name
    "VMName" = $vm.Name
    "VMSize" = $vm.HardwareProfile.VmSize
    "VMOSType" = $vm.OsType
    "VMProvisioningState" = $vm.ProvisioningState
    "VMLocation" = $vm.Location
    "VMOSDisk" = $vm.StorageProfile.OsDisk.Name
    }
    }
    }
    catch
    {
    Write-Host $error[0]
    }
    }
    //export to csv format
    $VMReport | Export-Csv "report.csv"

    2 people found this answer helpful.

  2. Sumit Sharma 11 Reputation points
    2022-11-11T16:54:39.58+00:00

    I ran into a similar issue and I was able to use a simple ForEach Loop to get this working. This will loop through each active subscription and find the virtual machines.

    $Subscriptions = Get-AzSubscription

    ForEach ($Subscription in $Subscriptions) {
    if($Subscription.State -eq "Enabled")
    {
    Set-AzContext -SubscriptionName $Subscription.Name
    $AzVM+=Get-AzVM -Status
    }
    }
    I hope this code helps someone in the future =]

    2 people found this answer helpful.

  3. Limitless Technology 39,351 Reputation points
    2022-09-09T07:38:56.35+00:00

    Hello there,

    This script will collect all VMs including the status, OS Type, Version, VM, Location, Resorce Group and Subscription Name.

    Define Variables($Subscription) to collect subscription details and $Report to store all VM status along with OS Type, OS Version, VM Name, RG Name.

    $Subscriptions = Get-AzureRmSubscription | Where-Object { $_.Name -in ("Prod", "Dev") }
    $Report = ForEach ($Subscription in $Subscriptions) {
    $SubscriptionName = $Subscription.Name
    Set-AzureRmContext -SubscriptionName "$SubscriptionName" | Out-Null
    $RGs = Get-AzureRMResourceGroup
    foreach ($RG in $RGs) {
    $VMs = Get-AzureRmVM -ResourceGroupName $RG.ResourceGroupName
    foreach ($VM in $VMs) {
    # VM Status (running/deallocated/stopped)
    $VMDetail = Get-AzureRmVM -ResourceGroupName $RG.ResourceGroupName -Name $VM.Name -Status
    $VMStatusDetail = $VMDetail.Statuses.DisplayStatus -match "^VM .*$"
    New-Object psobject -Property @{
    "SubscriptionName" = $SubscriptionName
    "VMName" = $VM.Name
    "VMStatus" = "$VMStatusDetail"
    "OSType" = $VM.StorageProfile.OSDisk.OSType
    "OSVersion" = $Vm.StorageProfile.ImageReference.Sku
    "ResourceGroup" = $RG.ResourceGroupName
    "Location" = $VM.Location
    }
    }
    }
    }

    End Subscription

    Pull the $Report variable to get all details and save in csv format.

    $Report | Export-Csv "c:\users\$env:username\documents\Azure_VMs_Status.csv" -Force -NoTypeInformation

    I hope this information helps. If you have any questions please let me know and I will be glad to help you out.


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