Worklet: Get Hard Drive Free Space Percentage

  • 11 October 2019
  • 2 replies
  • 50 views

Userlevel 5

Checks the percentage of free space on all drives. Only run manually. The evaluation code will always return true. The output will be in your activity log. Edit the variable $SpaceAlert to equal the percentage to report on. If the percentage free is greater then this number nothing will happen. If its lower it will echo to your activity log.

• Under Evaluation Code:


If (Test-Path "$env:allusersprofile\SoftwareDistribution")
{
Exit 0

}
Else {
Exit 1
}

• Under Remediation Code:


$SpaceAlert = '10'
$disks = get-wmiobject -class "Win32_LogicalDisk" -namespace "root\CIMV2"

$results = foreach ($disk in $disks)
{
if ($disk.Size -gt 0)
{
$size = [math]::round($disk.Size/1GB, 0)
$free = [math]::round($disk.FreeSpace/1GB, 0)
$freespace = ($free/$size) * 100
$freespace = [math]::Round($freespace)
}
}
If ($freespace >$SpaceAlert) {
Exit 0
}
Else {
Write-Output "Free space " $freespace"%"
}

2 replies

HI WEBBY

The foundation to checking drive space is awesome!
I may be missing something relatively easy, but this isn’t reporting all my systems drives.
Any ideas?

Reply