79828128

Date: 2025-11-23 21:43:44
Score: 1
Natty:
Report link

i'm surprised nobody mentioned tasklist
it seems almost tailor made to do this since you can just apply a filter to only pull processes by username:
tasklist.exe /fi "USERNAME eq $env:USERNAME"
will spit out all processes by the current user. no fuss, no admin, no wmi

it spits out an array of text so it's very easy to access and manipulate
want the nth task? just slap the index next to the command
(tasklist.exe /fi "USERNAME eq $env:USERNAME")[i]
looking for a particular process? pipe it into findstr
tasklist.exe /fi "USERNAME eq $env:USERNAME" | findstr.exe /i /r /c:"task*"
here's my oneliner to pull the pid of a desired task from the current user*
((tasklist.exe /fi "USERNAME eq $env:USERNAME" /FO CSV | findstr.exe /i /r /c:"task*") -replace('"\S+",("\d{2,}").*', '$1')) -replace ('"(\d+).*', '$1')
which of course you can then pass to either
Stop-Process -Id $(((tasklist.exe /fi "USERNAME eq $env:USERNAME" /FO CSV | findstr.exe /i /r /c:"task*") -replace('"\S+",("\d{2,}").*', '$1')) -replace ('"(\d+).*', '$1'))
or a more graceful taskkill
taskkill.exe /PID $(((tasklist.exe /fi "USERNAME eq $env:USERNAME" /FO CSV | findstr.exe /i /r /c:"task*") -replace('"\S+",("\d{2,}").*', '$1')) -replace ('"(\d+).*', '$1'))

*explanation
get the all of the current user's tasks
tasklist.exe /fi "USERNAME eq $env:USERNAME"
format the output as comma separated (gives us anchors when manipulating the output with regex)
/FO CSV
find your desired task findstr.exe /i /r /c:"task*"
/i case-insensitive
/r regex
/c: literal string
this first replace will spit out the PID with quotations thanks to the CSV format
replace('"\S+",("\d{2,}").*', '$1')
so you'd get something like "1234" hence, the second replace to clean the quotation marks
replace ('"(\d+).*', '$1')

if you want all the PIDs of the current user for some reason, here you go
((tasklist.exe /fi "USERNAME eq $env:USERNAME" /FO CSV) -replace('"\S+",("\d{2,}").*', '$1')) -replace ('"(\d+).*', '$1')

anyway, cheers hope this helps someone in the future!!

Reasons:
  • Blacklisted phrase (0.5): thanks
  • Blacklisted phrase (1): cheers
  • Whitelisted phrase (-1): hope this helps
  • Long answer (-1):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Unregistered user (0.5):
  • Low reputation (1):
Posted by: JD2005