Skip to content

Instantly share code, notes, and snippets.

@abextm
Created December 14, 2017 11:39
Show Gist options
  • Save abextm/62142caf3ab89e529e43773120d44a7c to your computer and use it in GitHub Desktop.
Save abextm/62142caf3ab89e529e43773120d44a7c to your computer and use it in GitHub Desktop.
show memory usage by process name
@ECHO OFF
powershell -ExecutionPolicy Bypass -command "gc %~dp0%0.bat | select -skip 3" | powershell -ExecutionPolicy Bypass -command -
exit /B
#We are now in powershell
$procs = @{}
Get-Process | ForEach-Object {
$obj = $procs.Get_Item($_.Name)
if(!$obj){
$obj = New-Object PSObject
$obj | Add-Member -type NoteProperty -Name Name -Value $_.Name
$obj | Add-Member -type NoteProperty -Name Count -Value 0
$obj | Add-Member -type NoteProperty -Name PrivateMemory -Value 0
}
$obj.PrivateMemory+=$_.PrivateMemorySize64
$obj.Count++
$procs.Set_Item($_.Name,$obj)
}
function fmt($v){
$suffix=""
if($v -gt 9999){
$suffix="Ki"
$v/=1024
}
if($v -gt 9999){
$suffix="Mi"
$v/=1024
}
if($v -gt 9999){
$suffix="Gi"
$v/=1024
}
if($v -gt 9999){
$suffix="Ti"
$v/=1024
}
$v=[math]::Round($v)
$b="B"
return "$v $suffix$b"
}
$procs.GetEnumerator() | %{ $_.Value } | Sort-Object -desc PrivateMemory | % { $_.PrivateMemory=fmt($_.PrivateMemory);$_ }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment