Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort the tasklist in the command prompt?

I just wanted to know how to make the MEMUSAGE column be in decreasing or increasing order. I searched all over the web and still did not find the answer, the closest point I got was this:

tasklist | sort

But when I try to use the /m for sort...:

tasklist | sort /m
Invalid switch.

Thanks for any help.

like image 515
Slackware Avatar asked Mar 12 '12 03:03

Slackware


People also ask

How do I sort tasklist in CMD?

The default is to use the system temporary directory. /O[UTPUT] [drive3:][path3]filename3 Specifies the file where the sorted input is to be stored. If not specified, the data is written to the standard output. Specifying the output file is faster than redirecting standard output to the same file.

What is tasklist command in CMD?

Tasklist is a tool that displays a list of the processes that are running on either a local or remote machine.

How do I sort files in CMD?

The command produces a sorted list of lines that contain the specified text. Then type the text that you want sorted, and press ENTER at the end of each line. When you have finished typing text, press CTRL+Z, and then press ENTER. The sort command displays the text you typed, sorted alphabetically.

How list all processes in CMD?

Use Of Tasklist Command Click on the cmd utility icon; it opens a command-line window. Type Tasklist in it and press the enter key. This command shows all the running processes in your system.


4 Answers

C:\> tasklist | sort /R /+58

The magic number 58 depend on your output.
Sorts the file according to characters in column 58(No guarantee!).

Note: I also have demand to sort TASKLIST by the column "Mem Usage". It is a bit tired to me when sorting not friendly by nth column, it is n character

On my computer, the column "Mem Usage" is the column 68th, I have to paste it into the Notepad to help for calculating correctly. Here is the command line:

tasklist | sort /R /+68
like image 169
kev Avatar answered Oct 15 '22 07:10

kev


Enable the /NH option in TASKLIST before piping to SORT. This suppresses the table header which messes with SORT.

TASKLIST /NH | SORT
like image 42
Jerade Hensley Avatar answered Oct 15 '22 07:10

Jerade Hensley


Actually, all you need to do is type the following:

tasklist | sort (This will sort the list items in ascending order)

tasklist | sort /R (This will sort the list items in descending order)

If you type: sort /? you can see where /R lists items in descending order, see below for details:

C:\windows\system32>sort /?
SORT [/R] [/+n] [/M kilobytes] [/L locale] [/REC recordbytes]
  [[drive1:][path1]filename1] [/T [drive2:][path2]]
  [/O [drive3:][path3]filename3]
  /+n                         Specifies the character number, n, to
                              begin each comparison.  /+3 indicates that
                              each comparison should begin at the 3rd
                              character in each line.  Lines with fewer
                              than n characters collate before other lines.
                              By default comparisons start at the first
                              character in each line.
  /L[OCALE] locale            Overrides the system default locale with
                              the specified one.  The ""C"" locale yields
                              the fastest collating sequence and is
                              currently the only alternative.  The sort
                              is always case insensitive.
  /M[EMORY] kilobytes         Specifies amount of main memory to use for
                              the sort, in kilobytes.  The memory size is
                              always constrained to be a minimum of 160
                              kilobytes.  If the memory size is specified
                              the exact amount will be used for the sort,
                              regardless of how much main memory is
                              available.

                              The best performance is usually achieved by
                              not specifying a memory size.  By default the
                              sort will be done with one pass (no temporary
                              file) if it fits in the default maximum
                              memory size, otherwise the sort will be done
                              in two passes (with the partially sorted data
                              being stored in a temporary file) such that
                              the amounts of memory used for both the sort
                              and merge passes are equal.  The default
                              maximum memory size is 90% of available main
                              memory if both the input and output are
                              files, and 45% of main memory otherwise.
  /REC[ORD_MAXIMUM] characters Specifies the maximum number of characters
                              in a record (default 4096, maximum 65535).
  /R[EVERSE]                  Reverses the sort order; that is,
                              sorts Z to A, then 9 to 0.
  [drive1:][path1]filename1   Specifies the file to be sorted.  If not
                              specified, the standard input is sorted.
                              Specifying the input file is faster than
                              redirecting the same file as standard input.
  /T[EMPORARY]
    [drive2:][path2]          Specifies the path of the directory to hold
                              the sort's working storage, in case the data
                              does not fit in main memory.  The default is
                              to use the system temporary directory.
  /O[UTPUT]
    [drive3:][path3]filename3 Specifies the file where the sorted input is
                              to be stored.  If not specified, the data is
                              written to the standard output.   Specifying
                              the output file is faster than redirecting
                              standard output to the same file.

This was tested on a Windows 2008 server and Windows 7 SP1

like image 40
ez4sheezee Avatar answered Oct 15 '22 07:10

ez4sheezee


To sort results by PID numbers :

C:\> tasklist /NH | sort /R /+29

/NH is used for "no header" - it skips the header of the tasklist view

/R is used for reverse sorted order (descending)

/+29 means that the reverse sorted order starts from the 30th character (so after the 29th character)

This works on all Windows CMD prompt on Server, Home or Pro versions.

like image 20
ngenne Avatar answered Oct 15 '22 06:10

ngenne