Skip to content

Instantly share code, notes, and snippets.

@jahio
Created March 31, 2022 03:30
Show Gist options
  • Save jahio/516433589236766a1d5873598bd4f8a3 to your computer and use it in GitHub Desktop.
Save jahio/516433589236766a1d5873598bd4f8a3 to your computer and use it in GitHub Desktop.
Gimme all processes using >= 200MB Working Set Size (64-bit) on...well, any OS that can run PowerShell Core (Linux, Mac, Windows)
#!/usr/bin/env pwsh
# Uses Get-Process piped to some stuff to get a list of processes
# over a certain amount of memory and output those as JSON.
# Takes no arguments.
# NOTE: This threshold is defined as BYTES, not KILOBYTES like the shell script.
# Adjust the math accordingly.
$threshold = (200 * 1024) * 1024 # Don't report anything greater than 200MB
$procs = Get-Process | Where-Object { $_.WorkingSet64 -gt $threshold }
$procs | Select-Object -Property CommandLine,WorkingSet64,Id | ConvertTo-Json
# We can do this with PowerShell without tainting output redirection to file;
# not so with bash|zsh! Set $VerbosePreference = 'Continue' to see this.
Write-Verbose "Found $($procs.Count) processes exceeding threshold"
Write-Verbose "Threshold: $($threshold / 1024 / 1024)MB"
# Compare how smooth and easy this is with the shell script equivalent:
# https://gist.github.com/jahio/5eaacad1c23a00f96137fd13cf3a7b16
#
# I know which one I'd rather work with!
@tpill90
Copy link

tpill90 commented Sep 17, 2024

Just a heads up you can make this even more readable by replacing line 11 with $procs = Get-Process | Where-Object { $_.WorkingSet64 -gt 200mb }, and even remove line 10 all together. Powershell can take kb, mb, gb and turn it into an integer for you.

image

@jahio
Copy link
Author

jahio commented Sep 19, 2024

Ha! Oh-em-gee. Yet another way in which pwsh wins over the arcane insanity that is traditional sh | bash | zsh syntax!

Don't get me wrong - you can pry my POSIX-compatible operating system(s) from my cold, dead hands; but adding pwsh on top of that model is clearly just an epic win. Thanks for pointing this out, Tim!

@tpill90
Copy link

tpill90 commented Sep 19, 2024

No problem at all! I've been using powershell for over a decade now, and when I started doing more Linux I took a stab at using it as well and has been just fine for me. I wanted to say that your article fantastically describes the benefits of PS over bash, with concrete examples that aren't just "I don't like PS/bash" like a lot of articles do.

If I can suggest one piece of feedback, you might want to consider updating it to include info on Powershell's predictive intellisense link. This to me is the killer feature that is making me switch over to PS exclusively on Linux. I've been using it for some time on windows, especially the table view, and every time I have to work on my Linux server it is frustrating to not have it. To me one of the worst things of command line is having to remember the arcane syntax, as well as having no discoverability. And this solves both of them for me.

@jahio
Copy link
Author

jahio commented Sep 20, 2024

Do you mind if I ask, @tpill90, where did you see that article? Where ya readin' from?

Regardless of the answer, thank you very much for the kind words, but more importantly, for the engagement here, and for enlightening others who may stop by and see this at some point in the future!

@tpill90
Copy link

tpill90 commented Sep 25, 2024

I found your gists from here : I switched from bash to powershell and its going great. I made the assumption that the author of the article was the author of the gists as well. Didn't think to check both and compare 😄

@jahio
Copy link
Author

jahio commented Sep 28, 2024

Ah, yes, I did indeed write that article. When Qarik bought out Stark and Wayne and later made significant organizational changes, they changed all the authors names on existing articles. That's also why I asked - if it was this as I suspected, I literally can't get in there to change anything anymore. Good suggestion, though!

(Edited slightly because lawyers)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment