Last active
January 28, 2020 13:22
-
-
Save andrii-riabchun/13bd07bc4f02cb03c47e67f544d061c8 to your computer and use it in GitHub Desktop.
Show memory usage of Hyper-V machines and processes (merged with same name)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$isAdmin = ([Security.Principal.WindowsPrincipal] ` | |
[Security.Principal.WindowsIdentity]::GetCurrent() | |
).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) | |
$aliases = @{ | |
"Steam"=@("steamwebhelper", "Steam"); | |
"Microsoft Edge"=@("MicrosoftEdge", "MicrosoftEdgeCP", "msedge"); | |
"Service Host"=@("svchost"); | |
"Visual Studio"=@("devenv", "ServiceHub.RoslynCodeAnalysisService32"); | |
} | |
function From-Alias { | |
param([string]$name) | |
$found = $aliases.GetEnumerator() | | |
Where-Object {$_.Value.Contains($name)} | | |
Select-Object | |
if ($found -eq $null) { | |
return $name | |
} | |
return $found.Key | |
} | |
function AddToTable { | |
param( | |
[string]$key, | |
[int]$memory | |
) | |
$key = From-Alias $key | |
if (-not $all.ContainsKey($key)) { | |
$all.Item($key) = @{} | |
} | |
$all.Item($key).Count+=1 | |
$all.Item($key).Memory += $memory | |
} | |
$all = @{} | |
if ($isAdmin) { | |
Get-VM | % { AddToTable ($_.VMName+" [VM]") $_.MemoryAssigned } | |
} else { | |
Write-Host "Must be elevated to show VM usage" | |
} | |
Get-Process | | |
Group-Object ProcessName | | |
Select-Object -Property Group | | |
% {$_.Group | % { AddToTable $_.Name $_.WorkingSet } } | |
$all.GetEnumerator() | | |
Sort-Object { $_.Value.Memory } -Descending | | |
Select-Object -First 10 -Property Name, | |
@{Name="Assigned MB"; Expression={[math]::Truncate(($_.Value.Memory / 1MB)) }}, | |
@{Name="Instances"; Expression={ $_.Value.Count }} | | |
Format-Table |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment