By using this site, you agree to the Privacy Policy and Terms of Use.
Forums › SQL Server DBA Discussion › SQL Server Scripts › SQL Script to get Disk Space details
Tagged: DBAScripts, diskspace
This SQL script is designed to query and display information about the disk space on the server where your SQL Server instance is running. It uses a combination of T-SQL and PowerShell commands executed through xp_cmdshell
to gather disk space details. Here’s a breakdown of its functionalities and usage:
This script queries and displays disk space information for SQL Server:
DECLARE @srvName VARCHAR(255)
DECLARE @sql VARCHAR(400)
DECLARE @output TABLE (line VARCHAR(255))
-- Default server name, can be set to another server name
SET @srvName = @@SERVERNAME
IF CHARINDEX ('\', @srvName) > 0
SET @srvName = SUBSTRING(@srvName, 1, CHARINDEX('\',@srvName)-1)
SET @sql = 'powershell.exe -c "Get-WmiObject -ComputerName ' + QUOTENAME(@srvName,'''') + ' -Class Win32_Volume -Filter ''DriveType = 3'' | SELECT name,capacity,freespace | foreach{$_.name+''|''+$_.capacity/1048576/1024+''%''+$_.freespace/1048576/1024+''*''}"'
-- Inserting disk name, total space, and free space value into temporary table
INSERT @output
EXEC xp_cmdshell @sql
-- Retrieve the values in GB from PowerShell Script output
SELECT @@servername as Hostname, RTRIM(LTRIM(SUBSTRING(line,1,CHARINDEX('|',line) -1))) AS drivename
,ROUND(CAST(RTRIM(LTRIM(SUBSTRING(line,CHARINDEX('|',line)+1,
(CHARINDEX('%',line) -1)-CHARINDEX('|',line)) )) AS FLOAT),0) AS 'capacity(GB)'
,ROUND(CAST(RTRIM(LTRIM(SUBSTRING(line,CHARINDEX('%',line)+1,
(CHARINDEX('*',line) -1)-CHARINDEX('%',line)) )) AS FLOAT),0) AS 'freespace(GB)'
,CAST (((ROUND(CAST(RTRIM(LTRIM(SUBSTRING(line,CHARINDEX('%',line)+1,
(CHARINDEX('*',line) -1)-CHARINDEX('%',line)) )) AS FLOAT),0)))*100/
(ROUND(CAST(RTRIM(LTRIM(SUBSTRING(line,CHARINDEX('|',line)+1,
(CHARINDEX('%',line) -1)-CHARINDEX('|',line)) )) AS FLOAT),0)) AS INT) AS 'freespace %'
FROM @output
WHERE line LIKE '[A-Z][:]%'
ORDER BY drivename
@srvName
: Holds the server name. By default, it’s set to the current server name (@@SERVERNAME
), but you can modify it to target a different server.@sql
: Stores the PowerShell command to be executed.@output
: A table variable to temporarily hold the results returned by the PowerShell command.@srvName
contains a backslash (indicating a server instance), the script extracts just the server’s name.Get-WmiObject
) queries Win32_Volume class objects on the specified server.DriveType = 3
) and selects their name, capacity, and free space.xp_cmdshell
:
xp_cmdshell
executes the PowerShell command.@output
table.@output
table to extract drive name, capacity, free space, and calculates the free space percentage.SUBSTRING
, CHARINDEX
, RTRIM
, and LTRIM
.Hostname
), drive name, total capacity in GB, free space in GB, and free space as a percentage.xp_cmdshell
is enabled and the account executing the script has the necessary permissions.xp_cmdshell
as it can execute system-level commands and may pose a security risk.Overall, this script is a valuable tool for SQL Server DBAs to monitor server disk space directly from within SQL Server, particularly useful in environments where DBAs have more access to SQL Server than the underlying operating system.