About the script

This script uses Get-WmiObject to retrieve disk information from a list of servers. Only harddisks are examined, and for each drive the function lists the servername, driveletter, totalsize in GB, free space in GB and free space in percentage. By using a filter I colorize "problematic" lines - which in this case is defined as drives with less than 20 % free space.

The output looks like this (free(%) column here shown with 2 decimals - the uploaded version of the script only yields 1):


The execution of the script is wrapped in a While-loop because I need to run it using a specific (privileged) account - using runas /user:XX.


Improvements/to be done:

The heading is at present also highlighted - this was not the intention, so please comment if you know how to fix this.

A right-aligned or perhaps just 0-padded approach might work better for the numeric columns.



The script is testet on a Windows 2008 R2 workstation against servers running Windows 2008 and Windows 2003. I assume that it will work with any client running PowerShell 2, but I am not sure if it is able to collect the WMI information from any kind of Windows system - again, please comment if you know about this.



# showDisk() - function to display disk usage on a list of servers
# Reads the file "servers.txt" from the current directory (same as the ps1-file)
# and collects information on all harddrives for every server in the list.
# Servers.txt simply contain a single line with each server name
# The script highlights drives with less than 10 % free space in red, 
# and drives with 10-20 % free space in yellow. 
# The limits are controlled with the regex input to the two colorize-row lines
# Requirements: 
# - PowerShell 2.0 on the local machine (where the script is run from)
# - An account with sufficient privileges (typical use: runas /user:xx.its@hhk.dk <thisscript.ps1>)
Function showDisk(){
 Get-Content servers.txt | ForEach-Object {
	 Get-WmiObject Win32_LogicalDisk -filter "DriveType=3" -computer $_ } | 
	 	SELECT 	SystemName, 
				@{Name="size(GB)";Expression={"{0:N1}" -f($_.size/1gb)}},
				@{Name="freespace(GB)";Expression={"{0:N1}" -f($_.freespace/1gb)}},
				@{Name="free(%)";Expression={"{0:P1}" -f($_.freespace/$_.size)}} 	| `
					colorize-row -prop "free(%)" -regex "^[0-9]," | `
					colorize-row -prop "free(%)" -color "yellow" -regex "^1[0-9],.+\%$" | ft

# colorize-row() - filter to highlight output based on a specific property value
# Input parameters: 
# color : the chosen highlight color
# prop  : the property to examine
# regex : the regular expression to search for
# Source: http://scriptolog.blogspot.com/2007/09/colorize-matching-output-in-pipeline.html
# Via @ShayLevy
filter colorize-row{
        [string]$regex=$(throw "must supply regular expression pattern")

    # save current console colors

    if($_.$prop -match $regex){[console]::ForegroundColor=$color; $_}
    else{[console]::ForegroundColor=$fgc; $_}

    # revert to saved console colors

# "Main" 
# Function is called repeatedly in a while loop to stop powershell from exiting when
# the script is run using runat /user...
# An alternative option is to use the command line parameter -NoExit

$stopNow = 0;
While ($stopNow -ne 1){
	$stopNow = Read-Host "Press 1(+enter) to stop the script, 0 to continue"