Use Powershell to check disk space on remote computers and servers

Powershell – Check Disk Space on Remote Computers and Servers

Here is a little Powershell script I created that uses WMI calls to check disk space on remote computers. This time, instead of using Active Directory to get a list of computers, I have the script setup to loop through an array of computers (easily editable within the script) so that only the computers you want scanned, are actually scanned.

This script will return the following information from the hard drive: The drive letter, volume name, file system, capacity, free space and the free space formatted as percentage. The script is designed to ignore floppy/cd/dvd drives and scan only hard disks/storage drives.

disk

# Array of hosts to check hard disk space
$computers = "192.168.1.67", "192.168.1.100", "192.168.1.220", "myfileserver", "192.168.1.233" 

foreach($pc in $computers)
{ 
 # Check if the computer is online/reachable first! 
 if(Test-Connection -Count 1 "$pc" -EA SilentlyContinue)
 {
 write-Host `n "$pc" `n -ForegroundColor Green
 try 
 {
 # Convert bytes to GB
 $total = @{Label=“Size (GB)”; Expression={"{0:N0}GB" -f ($_.size / 1GB)}} 
 $free = @{Label=“Freespace (GB)”; Expression={"{0:N0}GB" -f ($_.freespace / 1GB)}}  
 $percentageFree = @{Label=“Free Space (%)”; Expression={"{0:N0}%" -f (($_.freespace / $_.size)*100 )}}  
 Get-WmiObject win32_LogicalDisk -ComputerName $pc -ErrorAction Stop | Where-Object {$_.size -gt 0} | Select-Object -property Caption, VolumeName, FileSystem, $total, $free, $percentageFree | Format-Table -Wrap -AutoSize * 
 } 
 catch 
 {
 write-Host `n "Sorry - It looks like the RPC server on $pc is unavailable." -ForegroundColor Red 
 } 
 }
 else 
 {
 write-Host `n "Sorry - It looks like $pc is offline." -ForegroundColor Red 
 }
}
 
Read-Host `n 'Press the Enter key to close script...'

 

 

2 Comments

  1. Ankit

    This is really very helpful. Thanks for the script.

    Reply
  2. George

    Hi
    Very helpful script.
    But how can we save the results in a file instead of just viewing them?

    Reply

Leave a Comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>