Skip to main content

Batch Script Process

Various Batch Script commands involving Windows processes will be discussed below.

Viewing the List of Running Processes

In Batch Script, the tasklist command can be used to get the list of processes currently running in a system.

Syntax
tasklist[/s system [/u username [/p [password]]] [/m [module] | /svc | /v] [/fi filter]
[/fo format] [/nh]

The options that can be presented to the tasklist command are described below.

OptionDescription
/s systemSpecifies the remote system to connect to
/u [domain\]userSpecifies the user context under which the command should execute.
/p [password]Specifies the password for the given user context. Prompts for input if omitted.
/m [module]Lists all tasks currently using the given exe/dll name. If the module name is not specified all loaded modules are displayed.
/svcDisplays services hosted in each process.
/vDisplays verbose task information.
/fi filterDisplays a set of tasks that match a given criteria specified by the filter.
/fo formatSpecifies the output format. Valid values: TABLE, LIST, CSV.
/nhSpecifies that the "Column Header" should not show in the output. Valid only for TABLE and CSV formats.

Examples

The command below provides a list of all processes running on the local system:

tasklist
Image Name PID Session Name Session# Mem Usage
========================= ======== ================ =========== ============
System Idle Process 0 Services 0 4 K
System 4 Services 0 272 K
smss.exe 344 Services 0 1,040 K
csrss.exe 528 Services 0 3,892 K
csrss.exe 612 Console 1 41,788 K
wininit.exe 620 Services 0 3,528 K
winlogon.exe 648 Console 1 5,884 K
services.exe 712 Services 0 6,224 K
lsass.exe 720 Services 0 9,712 K
svchost.exe 788 Services 0 10,048 K
svchost.exe 832 Services 0 7,696 K
dwm.exe 916 Console 1 117,440 K
nvvsvc.exe 932 Services 0 6,692 K
nvxdsync.exe 968 Console 1 16,328 K
nvvsvc.exe 976 Console 1 12,756 K
svchost.exe 1012 Services 0 21,648 K
svchost.exe 236 Services 0 33,864 K
svchost.exe 480 Services 0 11,152 K
svchost.exe 1028 Services 0 11,104 K
svchost.exe 1048 Services 0 16,108 K
wlanext.exe 1220 Services 0 12,560 K
conhost.exe 1228 Services 0 2,588 K
svchost.exe 1276 Services 0 13,888 K
svchost.exe 1420 Services 0 13,488 K
spoolsv.exe 1556 Services 0 9,340 K
note

You not only get the various processes running on the system, but also the memory usage of each process.

The following command kills the open notepad task, if open.

taskkill /f /im notepad.exe

The following command kills a process which has PID of 1234.

taskill /pid 1234

Killing a Particular Process

Batch Scripting also has the ability to start a new process. This is achieved by using the start command.

start "title" [/d path] [options] "command" [parameters]

where:

  • title Text for the CMD window title bar (required.)
  • path Starting directory.
  • command The command, batch file or executable program to run.
  • parameters The parameters passed to the command.

Following are the description of the options which can be presented to the start command.

OptionsDescription
/minStart window Minimized
/maxStart window maximized.
/lowUse IDLE priority class.
/normalUse NORMAL priority class.
/abovenormalUse ABOVENORMAL priority class.
/belownormalUse BELOWNORMAL priority class.
/highUse HIGH priority class.
/realtimeUse REALTIME priority class.

Example

The following command will run the batch script test.bat in a new window. The windows will start in the minimized mode and also have the title of “Test Batch Script”.

start "Test Batch Script" /Min test.bat

The following command will actually run Microsoft word in another process and then open the file test-a.txt in Microsoft Word.

START "" "C:\Program Files\Microsoft Office\Winword.exe" "D:\test\test-a.txt"

Starting a New Process

Example