usr/Clear-PsWorkingSet.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Set-Alias -Name clpws -Value Clear-PsWorkingSet
function Clear-PsWorkingSet {
  [CmdletBinding()]param($PSBoundParameters)

  end {
    New-Delegate kernel32 {
      bool SetProcessWorkingSetSize([ptr, int, int])
    }

    New-PsProxy $PSBoundParameters -Callback {
      !$_.Handle ? (
        Write-Verbose "$($_.ProcessName) ($(
          $_.Id)): cannot clear working set."

      ) : (
        "$($_.ProcessName) ($($_.Id
        )) : {0}"
 -f $kernel32.SetProcessWorkingSetSize.Invoke(
        $_.Handle, -1, -1
      ))
    }
  }
}

Export-ModuleMember -Alias clpws -Function Clear-PsWorkingSet