Update : updated script to support Azure Az powershell module.
Here is a powershell script to generate a report of Azure Managed VM disks in a subscription. It generates a csv file which includes Disk Size, SKU, and associated VM details.
All these information can be helpful when you are auditing your Azure environment and want to identify any big Premium unattached disk adding bills to your Azure consumption bill. Once you identify such unattached managed disk, as a next action item you would like to delete them. Refer to post on deleting unattached managed disk.
Before start using this script, make sure, you have Azure Az Module installed and imported on the system. For more information on how to install and configure Az module refer following article : https://docs.microsoft.com/en-us/powershell/azure/new-azureps-module-az?view=azps-5.5.0
Let me know if this is helpful!
$currentDir = $(Get-Location).Path
$oFile = "$($currentDir)\AzureDisk_Details.csv"
if(Test-Path $oFile){Remove-Item $oFile -Force}
"ResourceGroupName,DiskName,AttachedVM,TimeCreated,OSType,DiskSizeInGB,DiskSKU,Encrypted,ProvisioningState,Location" | Out-File $oFile -Append -Encoding ASCII
Get-AzDisk | ForEach-Object{
$rgName = $diskName = $timeCreated = $osType = $diskSize = $diskSku = $encrypted = $provState = $location = $managedBy = ""
$rgName = $_.ResourceGroupName
$diskName = $_.Name
$managedBy = if($_.ManagedBy){$_.ManagedBy.split("/")[-1]}
$timeCreated = $_.TimeCreated
$osType = $_.OsType
$diskSize = $_.DiskSizeGB
$diskSku = (Get-AzDisk -Name $diskName -ResourceGroupName $rgName | select Sku -ExpandProperty Sku).Name
$encrypted = $_.EncryptionSettings
$provState = $_.ProvisioningState
$location = $_.Location
"$rgName,$diskName,$managedBy,$timeCreated,$osType,$diskSize,$diskSku,$encrypted,$provState,$location" | Out-File $oFile -Append -Encoding ASCII
}
Download above script and save it with a .ps1 file extension. Open powershell console and, login to your Azure account using – Login-AzAccount. It will prompt you to enter your Azure credentials.
After you login to Azure, run the powershell script saved in previous step.
You will receive a csv file output like below: