Description
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, awesome.
I have modified it to get the disk size value in GB. That’s it 
Source Code
$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 (%)”
$c.cells.item(1,6) = "Name "
$d = $c.UsedRange
$d.Interior.ColorIndex = 19
$d.Font.ColorIndex = 11
$d.Font.Bold = $True
$d.EntireColumn.AutoFit()
$intRow = 2
$colComputers = get-content "C:\Servers.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) = “{0:N0}” -f ($objDisk.Size/1GB)
$c.Cells.Item($intRow, 4) = “{0:N0}” -f ($objDisk.FreeSpace/1GB)
$c.Cells.Item($intRow, 5) = “{0:P0}” -f ([double]$objDisk.FreeSpace/[double]$objDisk.Size)
$c.cells.item($introw, 6) = $objdisk.volumename
$intRow = $intRow + 1
}
}
$d.EntireColumn.AutoFit()
cls
5 Spice ups
Thanks for this but how can you get % used?
this is how "{0:P0} " -f (($objDisk.Size-$objDisk.FreeSpace)/$objDisk.Size)
Amended it slightly, the autofit wasn’t working for me, and have added a column for the volume name. Plus I learned a bit about using “{0:N0}” Thanks $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 (%)” $c.cells.item(1,6) = “Name " $d = $c.UsedRange $d.Interior.ColorIndex = 19 $d.Font.ColorIndex = 11 $d.Font.Bold = $True $d.EntireColumn.AutoFit() $intRow = 2 $colComputers = get-content C:\server.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) = “{0:N0}” -f ($objDisk.Size/1GB) $c.Cells.Item($intRow, 4) = “{0:N0}” -f ($objDisk.FreeSpace/1GB) $c.Cells.Item($intRow, 5) = “{0:P0}” -f ([double]$objDisk.FreeSpace/[double]$objDisk.Size) $c.cells.item($introw, 6) = $objdisk.volumename $intRow = $intRow + 1 } }
Andrew809, For Autofit, just add the following two lines in end of the script (bottom) (it worked for me) BTW, thanks for the amendment to get the volume label, its very cool now. $d.EntireColumn.AutoFit() cls
Script has been updated with the volume label and Autofit of excels rows.
Sweet script J.Malek!! Thanks! Quick question - any idea how I’d go about highlighting a cell in a colour (or changing the text colour) if the free space was below 5%? I’ve tried hacking away at your script but so far to no avail. Thanks! Scott
Hey, Scott. Here is how (for 10%)., for 5% = 0.05 if($x.Value() -lt 0.1) { $c.Cells.Item($intRow, 5).Interior.ColorIndex = 3 }
Hi, just tried the script on a 2008R2 Server and it its not working. Once you start it in the PS I get lots of Errors and when I hit ctrl+c the PS-Console quits as well. Has anyone an idea why?
@NeilsJ - This is probably because you haven’t set your execution policy for powershell to allow for “RemoteSigned” or “Unrestricted” Paste the following command into powershell when you have opened powershell as an administrator - Set-ExecutionPolicy RemoteSigned or - Set-ExecutionPolicy Unrestricted After you have done this, try running the PS1 by navigating to the folder you stored the PS1 file in and running it from there then post the results. @JMalek - Thank you for the great script, I am unsure where I should be putting the color coding option in at for your script to highlight in red once it is below 10% If you have a moment to help out I’d appreciate it. Thanks!
Im sorry for my Noob question - this script is great for what we need I have problems running it - simple ones probably Running on Windows server 2003 R2 I have set the policy to unerstricted I CD to local folder where script is saved - and run .\test.ps1 I have saved the servers.txt file in root of C drive like in the code. PS screen - nothing happens - bllinking cursor and empty line
This is an awesome script guys; thanks to everyone. I do have a noob question as I’m just starting to work with powershell. I would like to schedule this script to run monthly (which I can do) but the tricky part is that I would like to add a column for each server each month so I can see growth over a period of time. This way I would be better able to predict disk space usage.
Hi i have a problem… how can i see computers hardisk total space in the network using php?
How can you pass credentials (uname and password) so that systems in a DMZ can be authenticated against before the drive space is queried?
Hi! Thank you for the useful script. I do have one question I hope someone can help me with. Can this be modified to include mount points?
Hey, How do I add CPU Performance(%) and Check the RAM used and free space too. I find scripts showing main memory usage everywhere online , cant find RAM usage anywhere. And please, let me know how to find CPU performance in %, as I always get it in a single integer ‘1’. I would really appreciate your help. Thanks.
Thanks for the script - just what I was looking for. One issue is that when the script runs, it only shows the C drive for most of my VM’s, but on others it shows all 3. It is also completely skipping one server and I have verified the server name :). I made no changes to the script above, both the ps1 and Servers.txt reside at the C:\ level. PowerShell ISE is run as Administrator. Any help is appreciated!
I am using the same but adding this after line 5 $sheet = $Workbook.WorkSheets.add() $sheet = $Excel.Worksheets.Item(“Sheet1”) $date=(get-date).Day $sheet.Name = “$date”
Great Script . I simply copy the script and it works.