In this blog, I will share the script to retrieve the Azure resources inside the Azure subscription. This script iterates through each resource group inside an Azure subscription and retrieves each resource name resource type and resource tag and dumps the information inside a CSV file. This file is created in the current directory with the nameList_Of_All_Azure_Resources_DATE.csv.
coconnect-azAccount $date = Get-Date -UFormat("%m-%d-%y") $currentDir = $(Get-Location).Path $oFile = "$($currentDir)\List_Of_All_Azure_Resources_$($date).csv" if(Test-Path $oFile){ Remove-Item $oFile -Force } "SubscriptName, ResourceGroupName,ResourceName,ResourceType,Tags" | Out-File $oFile -Append -Encoding ascii Get-AzSubscription | ForEach-Object{ $subscriptionId = $_.Id $subscriptionName = $_.Name Set-AzContext -SubscriptionId $subscriptionId Get-AzResourceGroup | ForEach-Object{ $resourceGroupName = $_.ResourceGroupName Get-AzResource -ResourceGroupName $resourceGroupName | ForEach-Object{ $resourceName = $_.Name $resourceType = $_.ResourceType if(!([string]::IsNullOrEmpty($_.Tags))){ $tags = @() $_.Tags.GetEnumerator() |ForEach-Object { [string]$tags += $_.key+ "=" + $_.value+ ";" } } else{ $tags = "" } "$subscriptionName,$resourceGroupName,$resourceName,$resourceType,$tags" | Out-File $oFile -Append -Encoding ascii } } }
I hope this script will be useful for Azure deployments where we want to retrieve all the resources in a CSV file for planning purposes.