Disclaimer
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.

 

<#
*** THIS SCRIPT IS PROVIDED WITHOUT WARRANTY, USE AT YOUR OWN RISK ***
.DESCRIPTION
    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.

.NOTES
	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
	Requires:  
	Tested: Windows 10 (PS v5), Windows Server 2012R2 (PS v4)

.PARAMETER None
 
.EXAMPLE 

.INFORMATION
    For a list of Drive Types for the Win32_LogicalDisk class visit the link below
    https://technet.microsoft.com/en-us/library/ee176674.aspx
#>

# 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

 

4 Comments

  1. M Federlechner | | Reply

    Thanks, David! Nice work, but same Problem as these two guys above. What should like to look the computers.txt

    • David | | Reply

      It should have a computer name on each line.

  2. Martin Bluck | | Reply

    This looks good, could you please provide an example of what the computers.txt file should look like?

  3. runebjorndal | | Reply

    This script looks really usefull! Can you please provide an example on how the computers.txt file is supposed to look like? I tried it on my local client first with only my hostname inside but get the error:

    Get-Content : Cannot find path ‘C:\scripts\drive_info\computers.txt’ because it does not exist.
    At line:1 char:1
    + Get-Content “C:\scripts\drive_info\computers.txt”
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : ObjectNotFound: (C:\scripts\drive_info\computers.txt:String) [Get-Content], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand

    I’ve checked the file paths 🙂

    Thanx.
    Rune

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.