A disk alert has fired, or a log file has grown again, and you need to know which file it is, where it lives and whether anything stops it growing. Get-DatabaseFilesDetail answers that in one grid: every data and log file of every user database, with its path, size, growth setting and maximum.
To check one database quickly, query sys.database_files in that database. It returns the same file details for just that database, in a few seconds:
SELECT name,
type_desc,
physical_name,
size / 128 AS size_mb,
CASE WHEN is_percent_growth = 1 THEN CAST(growth AS varchar(10)) + '%'
ELSE CAST(growth / 128 AS varchar(10)) + ' MB' END AS auto_growth
FROM sys.database_files;
The script below is for when you want every database on the instance in one grid.
The Script
Run the following script against your SQL Server instance. It is read-only and needs VIEW ANY DEFINITION. Without it the script returns no rows, not an error.
/*
Script Name : Get-DatabaseFilesDetail
Category : storage-capacity-management
Purpose : Show per-file details for all user databases: path, size, max size, growth settings.
Author : Peter Whyte (https://sqldba.blog/dba-scripts-get-sql-server-database-file-details/)
Requires : VIEW ANY DEFINITION. Without it sys.master_files returns no rows, not an error.
HealthCheck : Yes
Notes : Skips database snapshots (sys.master_files shows their maximum size, not disk use).
*/
-- SAFE:ReadOnly
-- IMPACT:Low
SET NOCOUNT ON;
SET QUOTED_IDENTIFIER ON;
SELECT
d.name AS database_name,
d.state_desc AS db_state,
d.recovery_model_desc AS recovery_model,
mf.file_id,
mf.name AS logical_name,
mf.type_desc AS file_type,
LEFT(mf.physical_name, 1) AS drive_letter,
mf.physical_name AS physical_path,
CAST(mf.size * 8.0 / 1024 AS DECIMAL(12,2)) AS current_size_mb,
-- NULL = no maximum. An unlimited log is stored as 268435456 pages (2 TB), not -1: mapped to NULL too.
CASE
WHEN mf.max_size = -1 THEN NULL
WHEN mf.type = 1 AND mf.max_size = 268435456 THEN NULL
WHEN mf.max_size = 0 THEN CAST(mf.size * 8.0 / 1024 AS DECIMAL(12,2))
ELSE CAST(mf.max_size * 8.0 / 1024 AS DECIMAL(12,2))
END AS max_size_mb,
CASE mf.is_percent_growth
WHEN 1 THEN CAST(mf.growth AS VARCHAR(10)) + '%'
ELSE CAST(CAST(mf.growth * 8.0 / 1024 AS INT) AS VARCHAR(20)) + ' MB'
END AS auto_growth,
mf.is_percent_growth AS growth_is_percent,
mf.state_desc AS file_state
FROM sys.master_files AS mf
INNER JOIN sys.databases AS d ON d.database_id = mf.database_id
WHERE d.database_id > 4
AND d.source_database_id IS NULL /* database snapshot */
ORDER BY d.name, mf.type, mf.file_id;
It reads sys.master_files and returns one row per data and log file of every user database. Database snapshots are left out, because sys.master_files shows their maximum size, not what they use on disk.
Example Output

Run against the lab instance: 42 files across 21 user databases, and max_size_mb is NULL on every one of them, data and log, so nothing on this instance stops a file growing until its volume is full.
Run it from the DBA Tools repo
git clone https://github.com/peterwhyte-lgtm/dba-tools
cd dba-tools
.\Initialize-Environment.ps1
.\run.ps1 Get-DatabaseFilesDetailAdd -ServerInstance SQLSERVER01 to run against another server. Script file: Get-DatabaseFilesDetail.sql
Understanding the Results
Read max_size_mb and auto_growth first: together they say whether each file can still grow, and how.
max_size_mbauto_growthgrowth_is_percentdb_statefile_statedrive_letterphysical_pathcurrent_size_mbrecovery_modeldatabase_namelogical_namefile_idfile_typeFrequently Asked Questions
Should SQL Server database files use percentage growth?
Generally no. Fixed MB growth settings are easier to predict and manage, especially for larger databases.
Does this work on Azure SQL Database?
No. sys.master_files is not available on Azure SQL Database, so run a sys.database_files query inside each database instead. Azure SQL Managed Instance has sys.master_files and runs the script as it is.
How do I find SQL Server database file locations?
Querying sys.master_files provides the physical paths for all database files on an instance.
How do I check SQL Server database file sizes?
Database file sizes can be reviewed using sys.master_files, sys.database_files, or monitoring scripts such as this one.
Related
You may also find these useful:
- Database Sizes and Free Space, space used inside each file
- Database Free Space Summary, free space per database
- Transaction Log Size and Usage, how full each log is
- Autogrowth History, when files grew, and by how much
- Disk Space, free space on each volume
- Filegroup Space, space by filegroup
- VLF Counts, log fragmentation from small growth steps
- Filegroup Is Full (Error 1105), what a growth of 0 turns into
- Unable to Open the Physical File (Error 5120), file access
- How to Right-Size Database Files, size before growth bites
- Storage and Capacity (hub), every storage script
- DBA Scripts: The Complete Guide, every script on this site
Leave a Reply