Home » PowerShell » PowerShell script to check free disk spaces for servers

PowerShell script to check free disk spaces for servers


Sharing a useful PowerShell script to check free spaces for the drives against a list of servers and write the results to Excel.

made by yingli, I have modified it to get the disk size value in GB. That’s it 🙂

============================================================================================

#This script  can be used to check disk space on remote server and
#output will be generated in excel file automatically.
#Put the serverlist in .txt file. you can modify the path if you want

$erroractionpreference = “SilentlyContinue”
$a = New-Object -comobject Excel.Application
$a.visible = $True

$b = $a.Workbooks.Add()
$c = $b.Worksheets.Item(1)

$c.Cells.Item(1,1) = “Machine Name”
$c.Cells.Item(1,2) = “Drive”
$c.Cells.Item(1,3) = “Total size (GB)”
$c.Cells.Item(1,4) = “Free Space (GB)”
$c.Cells.Item(1,5) = “Free Space (%)”

$d = $c.UsedRange
$d.Interior.ColorIndex = 19
$d.Font.ColorIndex = 11
$d.Font.Bold = $True
$d.EntireColumn.AutoFit($True)

$intRow = 2

$colComputers = get-content “C:\serverlist.txt”
foreach ($strComputer in $colComputers)
{
$colDisks = get-wmiobject Win32_LogicalDisk -computername $strComputer -Filter “DriveType = 3″
foreach ($objdisk in $colDisks)
{
$c.Cells.Item($intRow, 1) = $strComputer.ToUpper()
$c.Cells.Item($intRow, 2) = $objDisk.DeviceID
$c.Cells.Item($intRow, 3) = $objDisk.Size/1GB
$c.Cells.Item($intRow, 4) = $objDisk.FreeSpace/1GB
$c.Cells.Item($intRow, 5) = ([double]$objDisk.FreeSpace/[double]$objDisk.Size)
$intRow = $intRow + 1
}
$d.EntireColumn.AutoFit()
cls
}

==========================================================================================

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: