GDI handles count per session

Number of GDI handles per process in a given user session
Version 1.2.2
Created on 2016-05-24
Modified on 2017-01-31
Created by Noel Mahnkopf
Downloads: 89

The Script Copy Script Copied to clipboard
# GDI objects
# Number of GDI handles per process
$ErrorActionPreference = "Stop"

$sig = @'
[DllImport("User32.dll")]
public static extern int GetGuiResources(IntPtr hProcess, int uiFlags);
'@

Add-Type -MemberDefinition $sig -name NativeMethods -namespace Win32

$processes = [System.Diagnostics.Process]::GetProcesses()
[int]$gdiHandleCount = 0
$AllDetails = @()
ForEach ($p in $processes) {
    Try {
        $gdiHandles = [Win32.NativeMethods]::GetGuiResources($p.Handle, 0)
        If ($gdiHandles -eq 0) {
          continue
        }
        $gdiHandleCount += $gdiHandles
        $HandleList = New-Object PSObject
        $HandleList| add-member -MemberType NoteProperty -Name "Process Name" -Value $p.Name
        $HandleList| Add-Member -MemberType NoteProperty -Name "PID" -Value ($p.Id)
        $HandleList| add-member -MemberType NoteProperty -Name "Handles" -Value $gdiHandles
        $AllDetails += $HandleList
    }
    Catch {
        # Write-Host "Error accessing $p.Name"
    }
}

Write-Host "Processes with GDI handles"
$AllDetails | Sort-Object "Process Name" | ft -auto
Write-Host "Total number of GDI handles: $gdiHandleCount"