-1

I'm going to write command in PowerShell, which will output my process name and virtual memory. I need to change my value vm to megabytes.

Get-Process| Sort VM | Format-Table -p name,VM

And I have this

Name                                                                                    VM
----                                                                                    --
Idle                                                                                     0
smss                                                                               3121152
System                                                                             6053888
AEADISRV                                                                          18485248
lsm                                                                               23670784
SearchFilterHost                                                                  24240128
svchost                                                                           27344896
svchost                                                                           27922432
wininit                                                                           31449088
svchost                                                                           34127872
armsvc                                                                            34938880
lsass                                                                             35885056
WUDFHost                                                                          36036608
csrss                                                                             36552704
winlogon                                                                          37531648
svchost                                                                           37638144

Also I need to change color if process has > 100 Mb VM

striker92
  • 101
  • 6

1 Answers1

3

The Format-Table cmdlet doesn't support coloring. What you can do is to change color for each row.

If you are willing to give up the nice table format, pass the results to pipeline and use Write-Host -ForegroundColor to specify colors on cell level. Like so,

gwmi win32_process | select name,processid,vm | % {
$params = @{ Object = $_ }
$mem = $params["Object"].VM/1MB
if([int]$mem -ge 100 ) {
    write-host -nonewline -foregroundcolor yellow $mem " "
} else {
    write-host -nonewline $mem " "
}
write-host $params["Object"].name " " $params["Object"].processid
}

Another an approach is based on saving the result on some temporary medium and add coloring later. This requires a bit more code. Petri has a nice example with more code than I'm willing to copypaste here.

vonPryz
  • 22,996
  • 7
  • 54
  • 65