NAME
    Get-FreeDisk

SYNOPSIS
    Report file system disk space usage.


SYNTAX
    Get-FreeDisk [[-Computername] <String[]>] [-MinPercFree <Int32>] [-Credential <PSCredential>] [<CommonParameters>]


DESCRIPTION
    Report file system disk space usage of one or multiple computers using WMI.
    Reported data contains caption, label, capacity, used space and percentage free
    per volume.


PARAMETERS
    -Computername <String[]>
        The name of the computer(s) to report from.
        Default value is "localhost"

    -MinPercFree <Int32>
        Only report volumes that have less than MinPercFree % free.
        Default value is "100"

    -Credential <PSCredential>
        Specifies a user account that has permission to perform this action.
        The default is the current user.

    -------------------------- EXAMPLE 1 --------------------------

    PS C:\>Get-FreeDisk

    Get disk space usage for current machine

    -------------------------- EXAMPLE 2 --------------------------

    PS C:\>Get-FreeDisk Computername machine01, machine02, machine03 -MinPercFree 30

    Get disk space usage for multiple machines reporting only volumes w/ less than 30% free space

    -------------------------- EXAMPLE 3 --------------------------

    PS C:\>$cred = Get-Credential
    PS C:\> Get-Content .\servers.txt | Get-FreeDisk -Credential $cred

   Get disk space usage for machines named in "servers.txt" using alternate user credentials

PowerShell
Edit|Remove
<#
	.SYNOPSIS
		Report file system disk space usage.

	.DESCRIPTION
		Report file system disk space usage of one or multiple computers using WMI.
		Reported data contains caption, label, capacity, used space and percentage free
		per volume.

	.PARAMETER  Computername
		The name of the computer(s) to report from.
		Default value is "localhost"

	.PARAMETER  MinPercFree
		Only report volumes that have less than MinPercFree % free.
		Default value is "100"

	.PARAMETER  Credential
		Specifies a user account that has permission to perform this action.
		The default is the current user.
		
	.EXAMPLE
		PS C:\> Get-FreeDisk 
		Get disk space usage for current machine
		
	.EXAMPLE
		PS C:\> Get-FreeDisk Computername machine01, machine02, machine03 -MinPercFree 30
		Get disk space usage for multiple machines reporting only volumes w/ less than 30% free space
		
	.EXAMPLE
		PS C:\> $cred = Get-Credential
		PS C:\> Get-Content .\servers.txt | Get-FreeDisk -Credential $cred
		Get disk space usage for machines named in "servers.txt" using alternate user credentials
		
#>

function Get-FreeDisk
{
	[CmdletBinding()]
	param(
		[Parameter(Position=0, ValueFromPipeline=$true)]
		[string[]]$Computername="localhost",
		[int]
		[ValidateRange(0,100)]
		$MinPercFree = 100,
		[Management.Automation.PSCredential]$Credential = ([Management.Automation.PSCredential]::Empty)
	)
	begin{
		[String[]]$computers = @()	
	}
	process{
		$computers += $Computername
	}
	end{
		Get-WmiObject -computername $computers -Credential $Credential `
		-Query "select __SERVER, Caption,Label,Capacity,FreeSpace from Win32_Volume where DriveType != 5 and Capacity > 0" | `
		Add-Member -Name Free -MemberType ScriptProperty -PassThru -Value {($this.FreeSpace/10000) / ($this.Capacity/1000000)} | `
		Where { $_.Free -lt $MinPercFree } | `
		sort __SERVER,Caption | `
		Format-Table @{Label="Computer"; Expression={$_.__SERVER}}, Caption, Label,`
		@{Label="Size/MB"; FormatString="{0,7:N0}"; Expression={$_.Capacity / 1mb}},`
		@{Label="FreeSpace/MB"; FormatString="{0,7:N0}"; Expression={$_.Freespace / 1mb}}, `
		@{Label="Free"; FormatString="{0,3:N0}%"; Expression={$_.Free}} -AutoSize
	}
}

new-alias -name df -value Get-FreeDisk -description "Get free disk information"