Get Local Disk Space Report PowerShell

I have a functions that can be used to easily get you a report on disk space usage for any number of systems. I’ve used this in the past to email myself a list of the top offenders each night. I typically generate a list of systems using Get-ADComputer, but in this example I’ll just construct an array manually. The function Get-LocalDiskInfo uses WMI to query Win32_LogicalDisk and filters out anything that is not a local disk. It then returns a custom disk info object that has the percent free information in it.

The Get-LocalDiskReport function simply runs the Get-LocalDiskInfo for a list of systems. It also sorts the resulting array by PercentFree so the systems with low free space show up at the top of the list.

Here I’m manually creating an array of system names with 4 Windows Servers in it. After passing that to Get-LocalDiskReport, you can see what machines are the worst offenders

9

Function Code:

Get-LocalDiskReport

function Get-LocalDiskInfo {

    [CmdletBinding()] param (
        [parameter(Mandatory=$true)] [string]$ComputerName,
        [parameter(Mandatory=$false)] [Management.Automation.PSCredential] $Credential
    )
    PROCESS {

        $results = @()

        try {
            
            if($Credential) {
                $localDisks = Get-WmiObject -Class Win32_LogicalDisk -ComputerName $ComputerName -Credential $Credential | ?{$_.DriveType -eq 3}
            }
            else {
                $localDisks = Get-WmiObject -Class Win32_LogicalDisk -ComputerName $ComputerName | ?{$_.DriveType -eq 3}
            }
            
            foreach ($localDisk in $localDisks) {
                $custDiskObj = New-Object PSObject -Property @{
                    SystemName = $ComputerName
                    DriveLetter = $localDisk.DeviceID.TrimEnd(':')
                    VolumeName = $localDisk.VolumeName
                    CapacityGB = [System.Math]::Round($localDisk.Size/1024/1024/1024, 2)
                    FreeSpaceGB = [System.Math]::Round($localDisk.FreeSpace/1024/1024/1024, 2)
                    PercentFree = $null
                }
                if($localDisk.FreeSpace -ne 0) {
                    $custDiskObj.PercentFree = [System.Math]::Round(($localDisk.FreeSpace*100/$localDisk.Size), 0)
                }
                else {
                    $custDiskObj.PercentFree = 0
                }
                
                $results += $custDiskObj
            }
        }
        catch {
            Write-Host "`n$_`n" -ForegroundColor Magenta
            return
        }
        return $results | select SystemName,DriveLetter,VolumeName,CapacityGB,FreeSpaceGB,PercentFree | sort PercentFree
    }
}

function Get-LocalDiskReport {
    [CmdletBinding()] param (
        [parameter(Mandatory=$true)] [array]$ComputerNameList,
        [parameter(Mandatory=$false)] [Management.Automation.PSCredential] $Credential
    )
    PROCESS {
    
        $results = @()
    
        $ComputerNameList | %{
            if($Credential) {
                $results += Get-LocalDiskInfo -ComputerName $_ -Credential $Credential
            }
            else {
                $results += Get-LocalDiskInfo -ComputerName $_
            }
        }
        
        return $results | sort PercentFree
    }
}

Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>