Home
%3CLINGO-SUB%20id%3D%22lingo-sub-369189%22%20slang%3D%22en-US%22%3EHow%20to%20get%20Azure%20SQL%20database%20size%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-369189%22%20slang%3D%22en-US%22%3E%0A%20%26lt%3Bmeta%20http-equiv%3D%22Content-Type%22%20content%3D%22text%2Fhtml%3B%20charset%3DUTF-8%22%20%2F%26gt%3B%3CSTRONG%3EFirst%20published%20on%20MSDN%20on%20Feb%2008%2C%202019%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%20There%20are%20multiple%20ways%20to%20achieve%20this%20and%20there%20are%20also%20some%20%3CSTRONG%3E%20storage%20types%20%3C%2FSTRONG%3E%20you%20should%20be%20aware%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20There%20is%20a%20good%20doc%20about%20this%20at%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fsql-database%2Fsql-database-file-space-management%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3E%20https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fsql-database%2Fsql-database-file-space-management%20%3C%2FA%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CEM%3E%20Understanding%20the%20following%20storage%20space%20quantities%20are%20important%20for%20managing%20the%20file%20space%20of%20a%20database.%20%3C%2FEM%3E%20%3CBR%20%2F%3E%3CUL%3E%3CBR%20%2F%3E%3CLI%3E%3CSTRONG%3E%20Data%20space%20used%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%3CUL%3E%3CBR%20%2F%3E%3CLI%3E%3CSTRONG%3E%20The%20amount%20of%20space%20used%20to%20store%20database%20data%20in%208%20KB%20pages.%20%3C%2FSTRONG%3E%20Generally%2C%20space%20used%20increases%20(decreases)%20on%20inserts%20(deletes).%20In%20some%20cases%2C%20the%20space%20used%20does%20not%20change%20on%20inserts%20or%20deletes%20depending%20on%20the%20amount%20and%20pattern%20of%20data%20involved%20in%20the%20operation%20and%20any%20fragmentation.%20For%20example%2C%20deleting%20one%20row%20from%20every%20data%20page%20does%20not%20necessarily%20decrease%20the%20space%20used.%3C%2FLI%3E%3CBR%20%2F%3E%3C%2FUL%3E%3CBR%20%2F%3E%3C%2FLI%3E%3CBR%20%2F%3E%3C%2FUL%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CUL%3E%3CBR%20%2F%3E%3CLI%3E%3CSTRONG%3E%20Data%20space%20allocated%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%3CUL%3E%3CBR%20%2F%3E%3CLI%3E%3CSTRONG%3E%20The%20amount%20of%20formatted%20file%20space%20made%20available%20for%20storing%20database%20data.%20%3C%2FSTRONG%3E%20The%20amount%20of%20space%20allocated%20grows%20automatically%2C%20but%20never%20decreases%20after%20deletes.%20This%20behavior%20ensures%20that%20future%20inserts%20are%20faster%20since%20space%20does%20not%20need%20to%20be%20reformatted.%3C%2FLI%3E%3CBR%20%2F%3E%3C%2FUL%3E%3CBR%20%2F%3E%3C%2FLI%3E%3CBR%20%2F%3E%3C%2FUL%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CUL%3E%3CBR%20%2F%3E%3CLI%3E%3CSTRONG%3E%20Data%20space%20allocated%20but%20unused%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%3CUL%3E%3CBR%20%2F%3E%3CLI%3E%3CSTRONG%3E%20The%20difference%20between%20the%20amount%20of%20data%20space%20allocated%20and%20data%20space%20used.%20%3C%2FSTRONG%3E%20This%20quantity%20represents%20the%20maximum%20amount%20of%20free%20space%20that%20can%20be%20reclaimed%20by%20shrinking%20database%20data%20files.%3C%2FLI%3E%3CBR%20%2F%3E%3C%2FUL%3E%3CBR%20%2F%3E%3C%2FLI%3E%3CBR%20%2F%3E%3C%2FUL%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CUL%3E%3CBR%20%2F%3E%3CLI%3E%3CSTRONG%3E%20Data%20max%20size%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%3CUL%3E%3CBR%20%2F%3E%3CLI%3E%3CSTRONG%3E%20The%20maximum%20amount%20of%20space%20that%20can%20be%20used%20for%20storing%20database%20data.%20%3C%2FSTRONG%3E%20The%20amount%20of%20data%20space%20allocated%20cannot%20grow%20beyond%20the%20data%20max%20size.%3C%2FLI%3E%3CBR%20%2F%3E%3C%2FUL%3E%3CBR%20%2F%3E%3C%2FLI%3E%3CBR%20%2F%3E%3C%2FUL%3E%3CBR%20%2F%3E%3CP%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F89110i77DC76AE000352B4%22%20%2F%3E%3C%2FP%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20There%20are%20multiple%20methods%20to%20query%20this%20data%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20In%20the%20%3CSTRONG%3E%20Portal%20%3C%2FSTRONG%3E%20is%20the%20easiest%20way%20to%20get%20this%20for%20a%20single%20database%20in%20the%20%3CSTRONG%3E%20overview%20%3C%2FSTRONG%3E%20blade%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F89111iAD3B00CD2C9F3B12%22%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Or%20in%20the%20database%20%3CSTRONG%3E%20Metrics%20%3C%2FSTRONG%3E%20blade%20you%20can%20also%20check%20the%20%3CSTRONG%3E%20historical%20information%20%3C%2FSTRONG%3E%20%2C%20listed%20as%20%3CSTRONG%3E%20Total%20Database%20Size%20%3C%2FSTRONG%3E%20%2C%20at%20this%20moment%20represents%20%3CSTRONG%3E%20Used%20Space%20%3C%2FSTRONG%3E%20.%20%3CBR%20%2F%3E%3CUL%3E%3CBR%20%2F%3E%3CLI%3EIts%20%3CSTRONG%3E%20NOT%20%3C%2FSTRONG%3E%20provided%20in%20this%20chart%20the%20%3CSTRONG%3E%20allocated%20space%3C%2FSTRONG%3E%3C%2FLI%3E%3CBR%20%2F%3E%3C%2FUL%3E%3CBR%20%2F%3E%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F89112i03514D7C97F6F159%22%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20You%20can%20also%20query%20%3CSTRONG%3E%20master%20%3C%2FSTRONG%3E%20or%20directly%20the%20%3CSTRONG%3E%20database%20%3C%2FSTRONG%3E%20using%20TSQL%20%3CBR%20%2F%3E%20--%20Connect%20to%20master%20%3CBR%20%2F%3E%20--%20Database%20data%20space%20used%20in%20MB%20%3CBR%20%2F%3E%20SELECT%20TOP%201%20storage_in_megabytes%20AS%20DatabaseDataSpaceUsedInMB%20%3CBR%20%2F%3E%20FROM%20sys.resource_stats%20%3CBR%20%2F%3E%20WHERE%20database_name%20%3D%20'db1'%20%3CBR%20%2F%3E%20ORDER%20BY%20end_time%20DESC%20%3CBR%20%2F%3E%20OR%20%3CBR%20%2F%3E%20--%20Connect%20to%20database%20%3CBR%20%2F%3E%20--%20Database%20data%20space%20allocated%20in%20MB%20and%20database%20data%20space%20allocated%20unused%20in%20MB%20%3CBR%20%2F%3E%20SELECT%20SUM(size%2F128.0)%20AS%20DatabaseDataSpaceAllocatedInMB%2C%20%3CBR%20%2F%3E%20SUM(size%2F128.0%20-%20CAST(FILEPROPERTY(name%2C%20'SpaceUsed')%20AS%20int)%2F128.0)%20AS%20DatabaseDataSpaceAllocatedUnusedInMB%20%3CBR%20%2F%3E%20FROM%20sys.database_files%20%3CBR%20%2F%3E%20GROUP%20BY%20type_desc%20%3CBR%20%2F%3E%20HAVING%20type_desc%20%3D%20'ROWS'%20%3CBR%20%2F%3E%20OR%20%3CBR%20%2F%3E%20--%20Connect%20to%20database%20%3CBR%20%2F%3E%20--%20Database%20data%20max%20size%20in%20bytes%20%3CBR%20%2F%3E%20SELECT%20DATABASEPROPERTYEX('db1'%2C%20'MaxSizeInBytes')%20AS%20DatabaseDataMaxSizeInBytes%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CSTRONG%3E%20For%20an%20elastic%20pool%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%20--%20Connect%20to%20master%20%3CBR%20%2F%3E%20--%20Elastic%20pool%20data%20space%20used%20in%20MB%20%3CBR%20%2F%3E%20SELECT%20TOP%201%20avg_storage_percent%20%2F%20100.0%20*%20elastic_pool_storage_limit_mb%20AS%20ElasticPoolDataSpaceUsedInMB%20%3CBR%20%2F%3E%20FROM%20sys.elastic_pool_resource_stats%20%3CBR%20%2F%3E%20WHERE%20elastic_pool_name%20%3D%20'ep1'%20%3CBR%20%2F%3E%20ORDER%20BY%20end_time%20DESC%20%3CBR%20%2F%3E%20OR%20%3CBR%20%2F%3E%20--%20Connect%20to%20master%20%3CBR%20%2F%3E%20--%20Elastic%20pools%20max%20size%20in%20MB%20%3CBR%20%2F%3E%20SELECT%20TOP%201%20elastic_pool_storage_limit_mb%20AS%20ElasticPoolMaxSizeInMB%20%3CBR%20%2F%3E%20FROM%20sys.elastic_pool_resource_stats%20%3CBR%20%2F%3E%20WHERE%20elastic_pool_name%20%3D%20'ep1'%20%3CBR%20%2F%3E%20ORDER%20BY%20end_time%20DESC%20%3CBR%20%2F%3E%20OR%20%3CBR%20%2F%3E%20Connect-AzureRmAccount%20%3CBR%20%2F%3E%20%23%20Resource%20group%20name%20%3CBR%20%2F%3E%20%24resourceGroupName%20%3D%20%22rg1%22%20%3CBR%20%2F%3E%20%23%20Server%20name%20%3CBR%20%2F%3E%20%24serverName%20%3D%20%22ls2%22%20%3CBR%20%2F%3E%20%23%20Elastic%20pool%20name%20%3CBR%20%2F%3E%20%24poolName%20%3D%20%22ep1%22%20%3CBR%20%2F%3E%20%23%20User%20name%20for%20server%20%3CBR%20%2F%3E%20%24userName%20%3D%20%22name%22%20%3CBR%20%2F%3E%20%23%20Password%20for%20server%20%3CBR%20%2F%3E%20%24password%20%3D%20%22password%22%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%23%20Get%20list%20of%20databases%20in%20elastic%20pool%20%3CBR%20%2F%3E%20%24databasesInPool%20%3D%20Get-AzureRmSqlElasticPoolDatabase%20%60%20%3CBR%20%2F%3E%20-ResourceGroupName%20%24resourceGroupName%20%60%20%3CBR%20%2F%3E%20-ServerName%20%24serverName%20%60%20%3CBR%20%2F%3E%20-ElasticPoolName%20%24poolName%20%3CBR%20%2F%3E%20%24databaseStorageMetrics%20%3D%20%40()%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%23%20For%20each%20database%20in%20the%20elastic%20pool%2C%20%3CBR%20%2F%3E%20%23%20get%20its%20space%20allocated%20in%20MB%20and%20space%20allocated%20unused%20in%20MB.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20foreach%20(%24database%20in%20%24databasesInPool)%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20%24sqlCommand%20%3D%20%22SELECT%20DB_NAME()%20as%20DatabaseName%2C%20%60%20%3CBR%20%2F%3E%20SUM(size%2F128.0)%20AS%20DatabaseDataSpaceAllocatedInMB%2C%20%60%20%3CBR%20%2F%3E%20SUM(size%2F128.0%20-%20CAST(FILEPROPERTY(name%2C%20'SpaceUsed')%20AS%20int)%2F128.0)%20AS%20DatabaseDataSpaceAllocatedUnusedInMB%20%60%20%3CBR%20%2F%3E%20FROM%20sys.database_files%20%60%20%3CBR%20%2F%3E%20GROUP%20BY%20type_desc%20%60%20%3CBR%20%2F%3E%20HAVING%20type_desc%20%3D%20'ROWS'%22%20%3CBR%20%2F%3E%20%24serverFqdn%20%3D%20%22tcp%3A%22%20%2B%20%24serverName%20%2B%20%22.database.windows.net%2C1433%22%20%3CBR%20%2F%3E%20%24databaseStorageMetrics%20%3D%20%24databaseStorageMetrics%20%2B%20%3CBR%20%2F%3E%20(Invoke-Sqlcmd%20-ServerInstance%20%24serverFqdn%20%60%20%3CBR%20%2F%3E%20-Database%20%24database.DatabaseName%20%60%20%3CBR%20%2F%3E%20-Username%20%24userName%20%60%20%3CBR%20%2F%3E%20-Password%20%24password%20%60%20%3CBR%20%2F%3E%20-Query%20%24sqlCommand)%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%23%20Display%20databases%20in%20descending%20order%20of%20space%20allocated%20unused%20%3CBR%20%2F%3E%20Write-Output%20%22%60n%22%20%22ElasticPoolName%3A%20%24poolName%22%20%3CBR%20%2F%3E%20Write-Output%20%24databaseStorageMetrics%20%7C%20Sort%20%60%20%3CBR%20%2F%3E%20-Property%20DatabaseDataSpaceAllocatedUnusedInMB%20%60%20%3CBR%20%2F%3E%20-Descending%20%7C%20Format-Table%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20If%20you%20want%20to%20collect%20the%20database%20size%20without%20connecting%20directly%20to%20SQL%20Server%20you%20can%20query%20%3CSTRONG%3E%20Azure%20Metrics%20%3C%2FSTRONG%3E%20%2C%20as%20said%20above%20(%20%3CSTRONG%3E%20Total%20Database%20Size%20%3C%2FSTRONG%3E%20%2C%20at%20this%20moment%20represents%20%3CSTRONG%3E%20Used%20Space%20%3C%2FSTRONG%3E%20)%20%3CBR%20%2F%3E%20Connect-AzureRmAccount%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20function%20Get-TotalDatabaseSizeKb%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20param(%24Resource)%20%3CBR%20%2F%3E%20%24Result%20%3D%20%24Resource%20%7C%20Get-AzureRmMetric%20-MetricName%20'storage'%20-WarningAction%20SilentlyContinue%20%3CBR%20%2F%3E%20%24DBSize%20%3D%20%24Result.Data%5B%24Result.Data.Count-2%5D.Maximum%20%3CBR%20%2F%3E%20%24DBSize%20%2F%201024%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%20%3CBR%20%2F%3E%20%23Single%20DB%20%3CBR%20%2F%3E%20%24DB%20%3D%20Get-AzureRmResource%20-ResourceGroupName%20%22GROUPNAME%22%20-Name%20%22SERVER%2FDATABASENAME%22%20%3CBR%20%2F%3E%20%24DBSize%20%3D%20Get-TotalDatabaseSizeKb%20%24DB%20%3CBR%20%2F%3E%20%22DB%20(%24(%24DB.Name))%20%24(%24DBSize)Kb%20or%20%24(%24DBSize%20%2F%201024)Mb%22%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%20%3CBR%20%2F%3E%20%23All%20DB%20%3CBR%20%2F%3E%20%24Databases%20%3D%20Get-AzureRmResource%20-ResourceGroupName%20%22GROUPNAME%22%20-ResourceType%20Microsoft.Sql%2Fservers%2Fdatabases%20%3CBR%20%2F%3E%20foreach%20(%24DB%20in%20%24Databases)%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20%24DBSize%20%3D%20Get-TotalDatabaseSizeKb%20%24DB%20%3CBR%20%2F%3E%20%22DB%20(%24(%24DB.Name))%20%24(%24DBSize)Kb%20or%20%24(%24DBSize%20%2F%201024)Mb%22%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-369189%22%20slang%3D%22en-US%22%3EFirst%20published%20on%20MSDN%20on%20Feb%2008%2C%202019%20There%20are%20multiple%20ways%20to%20achieve%20this%20and%20there%20are%20also%20some%20storage%20types%20you%20should%20be%20awareThere%20is%20a%20good%20doc%20about%20this%20at%26nbsp%3Bhttps%3A%2F%2Fdocs.%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-369189%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAzure%20SQL%20Database%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EPowerShell%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ESize%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ETSQL%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Not applicable
First published on MSDN on Feb 08, 2019
There are multiple ways to achieve this and there are also some storage types you should be aware

There is a good doc about this at https://docs.microsoft.com/en-us/azure/sql-database/sql-database-file-space-management



Understanding the following storage space quantities are important for managing the file space of a database.

  • Data space used

    • The amount of space used to store database data in 8 KB pages. Generally, space used increases (decreases) on inserts (deletes). In some cases, the space used does not change on inserts or deletes depending on the amount and pattern of data involved in the operation and any fragmentation. For example, deleting one row from every data page does not necessarily decrease the space used.






  • Data space allocated

    • The amount of formatted file space made available for storing database data. The amount of space allocated grows automatically, but never decreases after deletes. This behavior ensures that future inserts are faster since space does not need to be reformatted.






  • Data space allocated but unused

    • The difference between the amount of data space allocated and data space used. This quantity represents the maximum amount of free space that can be reclaimed by shrinking database data files.






  • Data max size

    • The maximum amount of space that can be used for storing database data. The amount of data space allocated cannot grow beyond the data max size.









There are multiple methods to query this data



In the Portal is the easiest way to get this for a single database in the overview blade





Or in the database Metrics blade you can also check the historical information , listed as Total Database Size , at this moment represents Used Space .

  • Its NOT provided in this chart the allocated space






You can also query master or directly the database using TSQL
-- Connect to master
-- Database data space used in MB
SELECT TOP 1 storage_in_megabytes AS DatabaseDataSpaceUsedInMB
FROM sys.resource_stats
WHERE database_name = 'db1'
ORDER BY end_time DESC
OR
-- Connect to database
-- Database data space allocated in MB and database data space allocated unused in MB
SELECT SUM(size/128.0) AS DatabaseDataSpaceAllocatedInMB,
SUM(size/128.0 - CAST(FILEPROPERTY(name, 'SpaceUsed') AS int)/128.0) AS DatabaseDataSpaceAllocatedUnusedInMB
FROM sys.database_files
GROUP BY type_desc
HAVING type_desc = 'ROWS'
OR
-- Connect to database
-- Database data max size in bytes
SELECT DATABASEPROPERTYEX('db1', 'MaxSizeInBytes') AS DatabaseDataMaxSizeInBytes


For an elastic pool
-- Connect to master
-- Elastic pool data space used in MB
SELECT TOP 1 avg_storage_percent / 100.0 * elastic_pool_storage_limit_mb AS ElasticPoolDataSpaceUsedInMB
FROM sys.elastic_pool_resource_stats
WHERE elastic_pool_name = 'ep1'
ORDER BY end_time DESC
OR
-- Connect to master
-- Elastic pools max size in MB
SELECT TOP 1 elastic_pool_storage_limit_mb AS ElasticPoolMaxSizeInMB
FROM sys.elastic_pool_resource_stats
WHERE elastic_pool_name = 'ep1'
ORDER BY end_time DESC
OR
Connect-AzureRmAccount
# Resource group name
$resourceGroupName = "rg1"
# Server name
$serverName = "ls2"
# Elastic pool name
$poolName = "ep1"
# User name for server
$userName = "name"
# Password for server
$password = "password"

# Get list of databases in elastic pool
$databasesInPool = Get-AzureRmSqlElasticPoolDatabase `
-ResourceGroupName $resourceGroupName `
-ServerName $serverName `
-ElasticPoolName $poolName
$databaseStorageMetrics = @()

# For each database in the elastic pool,
# get its space allocated in MB and space allocated unused in MB.

foreach ($database in $databasesInPool)
{
$sqlCommand = "SELECT DB_NAME() as DatabaseName, `
SUM(size/128.0) AS DatabaseDataSpaceAllocatedInMB, `
SUM(size/128.0 - CAST(FILEPROPERTY(name, 'SpaceUsed') AS int)/128.0) AS DatabaseDataSpaceAllocatedUnusedInMB `
FROM sys.database_files `
GROUP BY type_desc `
HAVING type_desc = 'ROWS'"
$serverFqdn = "tcp:" + $serverName + ".database.windows.net,1433"
$databaseStorageMetrics = $databaseStorageMetrics +
(Invoke-Sqlcmd -ServerInstance $serverFqdn `
-Database $database.DatabaseName `
-Username $userName `
-Password $password `
-Query $sqlCommand)
}
# Display databases in descending order of space allocated unused
Write-Output "`n" "ElasticPoolName: $poolName"
Write-Output $databaseStorageMetrics | Sort `
-Property DatabaseDataSpaceAllocatedUnusedInMB `
-Descending | Format-Table




If you want to collect the database size without connecting directly to SQL Server you can query Azure Metrics , as said above ( Total Database Size , at this moment represents Used Space )
Connect-AzureRmAccount

function Get-TotalDatabaseSizeKb
{
param($Resource)
$Result = $Resource | Get-AzureRmMetric -MetricName 'storage' -WarningAction SilentlyContinue
$DBSize = $Result.Data[$Result.Data.Count-2].Maximum
$DBSize / 1024
}

###########################################################################################################
#Single DB
$DB = Get-AzureRmResource -ResourceGroupName "GROUPNAME" -Name "SERVER/DATABASENAME"
$DBSize = Get-TotalDatabaseSizeKb $DB
"DB ($($DB.Name)) $($DBSize)Kb or $($DBSize / 1024)Mb"

###########################################################################################################
#All DB
$Databases = Get-AzureRmResource -ResourceGroupName "GROUPNAME" -ResourceType Microsoft.Sql/servers/databases
foreach ($DB in $Databases)
{
$DBSize = Get-TotalDatabaseSizeKb $DB
"DB ($($DB.Name)) $($DBSize)Kb or $($DBSize / 1024)Mb"
}