VBScript: How to Modify the Priority of a Running Process?


Let’s say that you want to modify the priority of any notepad, you could write a VBScript with the following content:

1
2
3
4
5
6
7
8
9
10
11
12
13
' Modify the Priority Of a Running Process
Const ABOVE_NORMAL = 32768
 
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
 
Set colProcesses = objWMIService.ExecQuery _
    ("Select * from Win32_Process Where Name = 'Notepad.exe'")
 
For Each objProcess in colProcesses
    objProcess.SetPriority(ABOVE_NORMAL) 
Next
' Modify the Priority Of a Running Process
Const ABOVE_NORMAL = 32768

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colProcesses = objWMIService.ExecQuery _
    ("Select * from Win32_Process Where Name = 'Notepad.exe'")

For Each objProcess in colProcesses
    objProcess.SetPriority(ABOVE_NORMAL) 
Next

Run the VBScript and all notepad.exe processes (if found any) will be promoted with a higher process priority. This is made possible via the WMI (Windows Management Interface) object. And Query is similar to SQL:

1
Select * from Win32_Process Where Name = 'Notepad.exe'
Select * from Win32_Process Where Name = 'Notepad.exe'

And the constant ABOVE_NORMAL is defined in MSDN.

IDLE = 64
BELOW_NORMAL = 16384
NORMAL = 32
ABOVE_NORMAL = 32768
HIGH_PRIORITY = 128
REAL_TIME = 256

The power of VBScript has been greatly enhanced by inherent support of WMI Object Function Calls. And this post talks about how to kill the processes by Name using VBScript.

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
259 words
Last Post: Code Refactoring - C/C++ Unnecessary Loop Replaced with Math Expression
Next Post: The Simple and Powerful Parallel Runner for Windows Scripting Hosts

The Permanent URL is: VBScript: How to Modify the Priority of a Running Process?

Leave a Reply