This is my personal blog. The opinions and views I express are my own. The information I provide is on an as-is basis. I make no representations as to accuracy, completeness, currentness, suitability, or validity of any information on this blog and will not be liable for any errors, omissions, or delays in this information or any losses, injuries or damages arising from its use.

Remotely retrieve disk size, FreeSpace and % of FreeSpace with PowerShell

This script uses the win32_LogicalDisk WMI Class to get Local Disk Information for one or multiple computers. For more information on the DriveTypes available with the Win32_LogicalDisk class visit: https://technet.microsoft.com/en-us/library/ee176674.aspx.

You could also expand on this script and add email capability and schedule it as a job or a scheduled task. That way it runs without human input on a scheduled basis and emails the results.

Information retrieved:
System Name, DeviceID, Volume Name, Size(GB), FreeSpace(GB), % FreeSpace(GB), and the date/time the script is executed.

Output options included: Out-Gridview, a Table, CSV file and an HTML file.


    Use the win32_LogicalDisk WMI Class to get Local Disk Information for one
    or multiple computers. 
    Information gathered: 
        System Name    DeviceID   Volume Name  Size(GB)   FreeSpace(GB)  % FreeSpace(GB)   Date

    Output options include Out-Gridview, a Table, CSV file and an HTML file.

	File Name: 
	Author: David Hall
	Contact Info: 
		Website: www.signalwarrant.com
		Twitter: @signalwarrant
		Facebook: facebook.com/signalwarrant/
		Google +: plus.google.com/113307879414407675617
		YouTube Subscribe link: https://www.youtube.com/c/SignalWarrant1?sub_confirmation=1
	Tested: Windows 10 (PS v5), Windows Server 2012R2 (PS v4)


    For a list of Drive Types for the Win32_LogicalDisk class visit the link below

# Change the $exportpath to whatever path you want the html and CSV files in. 
$exportPath = "C:\scripts\drive_info" # I change this to a central fileshare

# Your computers.txt will need to be in this folder.
$computers = Get-Content "C:\scripts\drive_info\computers.txt"

# This is only used for the HTML file output option.
# If you're not using HTML, you can delete this section
# Start HTML Output file style
$style = "<style>"
$style = $style + "Body{background-color:white;font-family:Arial;font-size:10pt;}"
$style = $style + "Table{border-width: 1px; border-style: solid; border-color: black; border-collapse: collapse;}"
$style = $style + "TH{border-width: 1px; padding: 2px; border-style: solid; border-color: black; background-color: #cccccc;}"
$style = $style + "TD{border-width: 1px; padding: 5px; border-style: solid; border-color: black; background-color: white;}"
$style = $style + "</style>"
# End HTML Output file style

$driveinfo = Get-WMIobject win32_LogicalDisk -ComputerName $computers -filter "DriveType=3" |
                Select-Object SystemName, DeviceID, VolumeName,
                @{Name="Size(GB)"; Expression={"{0:N1}" -f($_.size/1gb)}},
                @{Name="FreeSpace(GB)"; Expression={"{0:N1}" -f($_.freespace/1gb)}},
                @{Name="% FreeSpace(GB)"; Expression={"{0:N2}%" -f(($_.freespace/$_.size)*100)}},
                @{Name="Date"; Expression={$(Get-Date -format 'g')}} 

# Various Output Options
$driveinfo | Out-GridView 
$driveinfo | Format-Table -AutoSize
$driveinfo | Export-Csv "$exportPath\Server_Drivespace.csv" -NoTypeInformation -NoClobber -Append
$driveinfo | ConvertTo-HTML -head $style | Out-File $exportPath\Server_Drivespace.htm -NoClobber -Append