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"; } } |


[...] a PowerShell script here: Check disk space with Powershell | youdidwhatwithtsql.com Takes the servers from a TXT, outputs to screen and [...]
Hello Guy,
thank you so much… really this command or why not tool is awsome!
Thanks
EFO
I copied, and modified this script. Very well written. I’m a big Fan of Rhys
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
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
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
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