0

Check drive free space on a remote machine with PowerShell

Picture this scenario. You want to be able to check the free space on a server, but you don’t want to have to RDP onto the server in order to do this. One solution to this is to use PowerShell to check this for you and return the value.

The cmdlet Check-FreeSpace below will do this for you.

# This function returns the free space available on a 
# drive on a remote machine.
function Check-FreeSpace([string]$machine, [string]$drive)
{
	$freeSpace = "$machine $drive " + 
		(Get-WmiObject win32_logicaldisk -ComputerName $machine | 
			Where-Object { $_.DeviceID -eq $drive } | ForEach-Object {
				[math]::Round($_.freespace / 1GB,2)}) + " GB."
	return $freeSpace + "`n"
}

To use this cmdlet you can do the following:

Check-FreeSpace "servername" "F:"

Jason Edwards