Check disk space with Powershell

Need to monitor disk space on multiple servers? Then make the job easy with this Powershell script. To configure this script just create a file called serverlist.txt in your user profile folder, C:\Users\Rhys on my laptop. The $percentWarning variable allows you to control at what percentage level you will be warned about free disk space. If disk space is less than this then the text will be coloured red to draw your attention to it. The script will also output a datetime stamped csv file in your user folder containing similar data.

# Issue warning if % free disk space is less 
$percentWarning = 15;
# Get server list
$servers = Get-Content "$Env:USERPROFILE\serverlist.txt";
$datetime = Get-Date -Format "yyyyMMddHHmmss";
 
# Add headers to log file
Add-Content "$Env:USERPROFILE\server disks $datetime.txt" "server,deviceID,size,freespace,percentFree";
 
foreach($server in $servers)
{
	# Get fixed drive info
	$disks = Get-WmiObject -ComputerName $server -Class Win32_LogicalDisk -Filter "DriveType = 3";
 
	foreach($disk in $disks)
	{
		$deviceID = $disk.DeviceID;
		[float]$size = $disk.Size;
		[float]$freespace = $disk.FreeSpace;
 
		$percentFree = [Math]::Round(($freespace / $size) * 100, 2);
		$sizeGB = [Math]::Round($size / 1073741824, 2);
		$freeSpaceGB = [Math]::Round($freespace / 1073741824, 2);
 
		$colour = "Green";
		if($percentFree -lt $percentWarning)
		{
			$colour = "Red";
		}
		Write-Host -ForegroundColor $colour "$server $deviceID percentage free space = $percentFree";
		Add-Content "$Env:USERPROFILE\server disks $datetime.txt" "$server,$deviceID,$sizeGB,$freeSpaceGB,$percentFree";
	}
}

cid image001 png01c9f0dc thumb1 Check disk space with Powershell


13 Comments

  1. [...] a PowerShell script here: Check disk space with Powershell | youdidwhatwithtsql.com Takes the servers from a TXT, outputs to screen and [...]

  2. Eriko Oliveira says:

    Hello Guy,

    thank you so much… really this command or why not tool is awsome!

    Thanks
    EFO

  3. Jelly says:

    I copied, and modified this script. Very well written. I’m a big Fan of Rhys

  4. Aron says:

    Hey Thanks for script!

    This is almost exactly what I’m trying to do, except I need it to output the result of the script to the body of an email. I think what I need it to make the output into a variable I can call in the body of the email, in my example below I called it $DriveSpace.

    $SmtpClient = new-object system.net.mail.smtpClient
    $SmtpServer = “MAILSERVER”
    $SmtpClient.host =
    $SmtpServer

    $From = “USER ”
    $To = “ADDRESS@DOMAIN.COM”
    $Title = “Email Title”
    $Body = $DriveSpace
    $msg.IsBodyHTML = $true
    $SmtpClient.Send($from,$to,$title,$Body)

    I’m pretty new to Powershell and this is my first attempt at scripting, any advice would be appreciated :)

  5. Rhys says:

    Hi Aron,

    I also write the output into a text file so you can read the contents of this into a variable with the Get-Content cmdlet. Something like this should do it…
    $DriveSpace = Get-Content "$Env:USERPROFILE\server disks $datetime.txt";

    Cheers,

    Rhys

  6. Nathan says:

    Hey there, Rhys… awesome script, thank you!

    About your last comment… I’m a total beginner to PS, so could you explain a bit on how to put that $DriveSpace variable into the script properly in order to output all the results into a text file? I’ve tried it myself a couple of times, but I’m not having a lot of luck. My text file generates, but ends up with just “server,deviceID,size,freespace,percentFree” in it.

    Your assistance is much appreciated. Thanks!

    -Nathan

  7. Rhys says:

    Hi Nathan,

    Welcome to the awesome world of Powershell!

    The original script generates a text file so you don’t need to worry about that. The $DriveSpace variable was just a suggestion to get the content into an email. If you run the original script do you have a text file in your user directory called “server disks yyyymmddhhmiss.txt”? I’ve retested the script and it works fine for me. Try the original script and check your user folder for the file.

    Cheers,

    Rhys

  8. TJ says:

    I keep getting the error below for one of my servers. Any idea why?

    Get-WmiObject : Access is denied. (Exception from HRESULT: 0×80070005 (E_ACCESS
    DENIED))
    At C:\server_check.ps1:12 char:24
    + $disks = Get-WmiObject <<<< -ComputerName $server -Class Win32_LogicalDisk -Filter "DriveType = 3";

  9. Rhys says:

    Hi TJ,

    Looks like a permissions issue to me so check this on the server in question.

    Cheers,

    Rhys

  10. Stuart says:

    Great code. One issue: I’m getting NaN as a result for one of the logical drives. When I check manually on the server, I can see this drive’s usage (110 GB used out of 500 GB).

    Not sure why I’m getting a non-number result.

  11. Rhys says:

    Hi Stuart,
    Mmm, I’d output the variables in the calculation. NaN…

    “The value of this constant is the result of dividing zero by zero.” ref

    Perhaps it’s having trouble getting the values for this drive.

    Cheers,

    Rhys

  12. ErwinvA says:

    Great script, thanks for sharing.

    Erwin

  13. Jeff says:

    Thanks for the script. I was looking for something like this to monitor a server that fills up on occasion. You saved me a lot of time by publishing this script.

    Cheers.

    Jeff

Leave a Reply