<< Back to Script Library

GDI handles count per session

Number of GDI handles per process in a given user session
Version: 1.2.2
Created: 2016-05-24
Modified: 2017-01-31
Creator: Noel Mahnkopf
Downloads: 87
The Script Copy Script Copied to clipboard
# GDI objects
# Number of GDI handles per process
$ErrorActionPreference = "Stop"

$sig = @'
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) {
        $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"