SQL Server – How to Get Physical Disk ID and Drive Letters in SSMS

sql serverssms

Can anyone tell me how I can go about getting the physical disk id and drive letters of the local server from within management studio? I know I can grab the drive letters with xp_fixed drives and extrapolate to the full disk size with xp_cmdshell and fsutil, but what I want is something like the below:

╔══════════╦═════════════╦════════╦════════════╗
║ Drive_id ║ Dive Letter ║  Size  ║ Free_Space ║
╠══════════╬═════════════╬════════╬════════════╣
║        0 ║ C           ║  10240 ║       8000 ║
║        0 ║ E           ║ 102400 ║      25000 ║
║        1 ║ F           ║ 102400 ║      22000 ║
╚══════════╩═════════════╩════════╩════════════╝

Any ideas?

EDIT:
Just to be clear I have all of the pieces of the puzzle in the below code EXCEPT the physical drive_id. If anyone can think of a way to add it to the code below that would be fantastic – if I need to blow all that away and redo it to get the information I'm after I can live with that too.

Code So Far:

SET NOCOUNT ON
IF OBJECT_ID(N'tempdb..#FreeDiskSize') IS NOT NULL DROP TABLE #FreeDiskSize

CREATE TABLE #FreeDiskSize (name varchar(5), available_mb int)

INSERT #FreeDiskSize(name,available_mb) 
EXEC master..XP_FIXEDDRIVES
DECLARE @Drive VARCHAR(5), @CMD VARCHAR(1000), @pos SMALLINT

IF OBJECT_ID(N'tempdb..#TotalDiskSize') IS NOT NULL DROP TABLE #TotalDiskSize

CREATE TABLE #TotalDiskSize (TotalBytes VARCHAR(1000), Drive VARCHAR(5))

DECLARE Drive_name CURSOR FOR 

SELECT name FROM #FreeDiskSize
OPEN Drive_name
FETCH NEXT FROM Drive_name INTO @Drive
WHILE @@FETCH_STATUS = 0 
BEGIN
SET @CMD='MASTER..XP_CMDSHELL ' + ''''+ 'FSUTIL VOLUME DISKFREE ' + @Drive + ':| find '+ '"Total # of bytes"'+''''
INSERT #TotalDiskSize(TotalBytes) EXEC (@CMD)
UPDATE #TotalDiskSize SET Drive=@Drive WHERE Drive IS NULL
FETCH NEXT FROM Drive_name INTO @Drive
END
CLOSE Drive_name
DEALLOCATE Drive_name
DELETE FROM #TotalDiskSize WHERE TotalBytes IS NULL
SELECT @pos=charindex(':',TotalBytes) FROM #TotalDiskSize

SELECT b.Drive as Drive, CONVERT(BIGINT,(RIGHT(b.TotalBytes,(LEN(b.TotalBytes)-@pos))))/1073741824 AS TOTAL_Drive_SPACE_GB, a.available_mb/1024 AS AVAILABLE_SPACE_GB
FROM #FreeDiskSize a WITH (NOLOCK)
INNER JOIN #TotalDiskSize b WITH (NOLOCK)ON a.name=b.Drive

Best Answer

You can use glennberry scripts..

 SELECT DISTINCT vs.volume_mount_point, 
vs.file_system_type, 
    vs.logical_volume_name,
 CONVERT(DECIMAL(18,2),vs.total_bytes/1073741824.0) AS [Total Size (GB)],
    CONVERT(DECIMAL(18,2),vs.available_bytes/1073741824.0) AS [Available Size (GB)],  
    CAST(CAST(vs.available_bytes AS FLOAT)/ CAST(vs.total_bytes AS FLOAT) AS DECIMAL(18,2)) * 100 AS [Space Free %] 
    FROM sys.master_files AS f WITH (NOLOCK)
    CROSS APPLY sys.dm_os_volume_stats(f.database_id, f.[file_id]) AS vs OPTION (RECOMPILE);

Below is the sample output,is this what you need..

enter image description here