List the percentage of free disk space for multiple computers.
# Display the drive space on all drives
# if any have < 20% free space, log to a file for review
function DriveSpace {
param( [string] $strComputer)
"$strComputer ---- Free Space (percentage) ----"
# Does the server responds to a ping (otherwise the WMI queries will fail)
$query = "select * from win32_pingstatus where address = '$strComputer'"
$result = Get-WmiObject -query $query
if ($result.protocoladdress) {
# Get the Disks for this computer
$colDisks = get-wmiobject Win32_LogicalDisk -computername $strComputer -Filter "DriveType = 3"
# For each disk calculate the free space
foreach ($disk in $colDisks) {
if ($disk.size -gt 0) {$PercentFree = [Math]::round((($disk.freespace/$disk.size) * 100))}
else {$PercentFree = 0}
$Drive = $disk.DeviceID
"$strComputer - $Drive - $PercentFree"
# if < 20% free space, log to a file
if ($PercentFree -le 20) {"$strComputer - $Drive - $PercentFree" | out-file -append -filepath "C:\logs\Drive Space.txt"}
}
}
}
foreach ($computer in cat C:\batch\servers.txt) {DriveSpace "$computer"}
This assumes you have saved a list of computernames to check in the file 'servers.txt'
Example
Assuming the script above is saved in the current directory as Get-DriveSpace.ps1:
PS C:\> ./Get-DriveSpace
“In any collection of data, the figure most obviously correct, beyond all need of checking, is the mistake” ~ Finagle's third law.
Related PowerShell Cmdlets:
pshInfo - List Ram, Disk Space, Service pack, Uptime.