May this thread helps you: https://learn.microsoft.com/en-us/answers/questions/992897/powershell-script-to-get-list-of-all-vms-in-all-su Or this script as summary:
# Define the output CSV file path
$file = "C:\path\to\your\output.csv"
# Get all Azure subscriptions
$Subscriptions = Get-AzSubscription
# Initialize an array to store VM information
$VMs = @()
# Loop through each subscription
foreach ($Subscription in $Subscriptions) {
# Set the current subscription context
Set-AzContext -SubscriptionId $Subscription.Id
# Get all VMs in the subscription
$VMs += Get-AzVM
}
# Create a custom object for each VM
$VMReport = $VMs | ForEach-Object {
[PSCustomObject]@{
Subscription = $_.Context.Subscription.Name
ResourceGroup = $_.ResourceGroupName
VMName = $_.Name
}
}
# Export the VM information to CSV
$VMReport | Export-Csv -Path $file -NoTypeInformation
# Display a message
Write-Host "VM information exported to $file"
** Please don't forget to close up the thread here by upvoting and accept it as an answer if it is helpful **