This script Checks a list of remote computers. You specify the servers inside of the $servers variable by placing each in quotes with a comma between entries as I have done below within the script. All you must do then is simply run the script. WMI will discover all drives on the remote machine and give you the total size, used space, free space and percent free. Entries below 15% will be shown in red and all others will be shown in yellow. You can add as many servers as you like, or easily mod the file to read from a text file.
You will probably want to save your credentials with this script as I posted about here for ease of use.
I use this particular script a lot.
#Retrieve Drive Space on Remote Computers#SET TARGET SERVERS
$servers = "Server1", "Server2", "Server3"$gb = 1024 * 1024 * 1024
foreach ($target in $servers){
write-host "Checking $target..."
$obj = (gwmi -query "select * from win32_logicaldisk where Size > 1" -computername $target -cred $pw)
foreach ($disk in $obj) {
$freespace = ($disk.freespace / $gb)
$totalspace = ($disk.Size / $gb)
$freepercent = (($freespace / $totalspace)*100)
if ($freepercent -lt "15") {
write-host "Drive" ($disk.deviceid) "has" ($disk.freespace / $gb).ToString("n") "GB Free of" ($disk.Size / $gb).ToString("n") "GB." $freepercent.ToString("n") "% Free." -foregroundcolor red
}
else {
write-host "Drive" ($disk.deviceid) "has" ($disk.freespace / $gb).ToString("n") "GB Free of" ($disk.Size / $gb).ToString("n") "GB." $freepercent.ToString("n") "% Free." -foregroundcolor green
}
}
}
There is no need for variable $gb. You can just divide $disk.freespace/1GB. Type 1KB (or 1kb; it’s case insensitive), 1MB or 1GB at PowerShell prompt and see what you will get.
From the help file:
Windows PowerShell can also convert kilobytes (KB), megabytes (MB), and gigabytes (GB) into bytes when assigning a value to a variable. For example, to assign 10 kilobytes to the $a variable, type:
$a = 10kb
When you run this command, Windows PowerShell assigns a value of 10,240 to the variable.