How Can I Lower the Priority of a Process Using a Script?

How Can I Lower the Priority of a Process Using a Script?

  • Comments 2
  • Likes
Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I lower the priority of a process using a script?

-- HS

SpacerHey, Scripting Guy! AnswerScript Center

Hey, HS. As many of you know, all processes are not created equal; instead processes are assigned priority values. Processes with a high priority are executed more often (and thus appear to run faster) than processes with a lower priority. For example, both the keyboard and mouse run in a system process with a very high priority; if they did not you would notice a delay each time you typed a key on the keyboard or moved the mouse.

Generally speaking most processes run with a Normal priority: not too fast, not too slow. That seems to work out pretty good. However, there might be times when you want to change the priority of a particular process. For example, late at night you might want to increase the priority of the Process X; that way you can ensure that whatever task the process is carrying out gets completed faster. Conversely, if Process X is running during the middle of the day you might want to decrease the priority; that way the process won’t interfere with other tasks being carried out on the computer. That’s a quick overview and there are obviously other things to consider before you start changing process priorities. But at least you know what we’re talking about.

So let’s assume that you do want to change the priority of a process. You can do that quite easily using Task Manager; just right-click the process, choose Set Priority, and then set one of the available options:

Process Priority


Nice. But can you do this same thing using a script?

Well, as long as you’re running Windows XP or Windows Server 2003 the answer is yes. (Sorry, Windows 2000 users, but this won’t work for you.) That’s because the WMI class Win32_Process, found in those versions of Windows, includes a method named SetPriority, which allows you to modify the priority of a running process. For example, here’s a script that sets the priority of Notepad.exe to Below Normal:

Const BELOW_NORMAL = 16384

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colProcesses = objWMIService.ExecQuery _
    ("Select * from Win32_Process Where Name = 'Notepad.exe'")
For Each objProcess in colProcesses
    objProcess.SetPriority(BELOW_NORMAL) 
Next

The script begins by defining a constant named BELOW_NORMAL and setting the value to 16384; as you might expect, we’ll use this constant later on to change the priority of Notepad to Below Normal. Are there other values you can use to assign a different priority to a process? Of course there are:

Priority Class

Value

Normal

32

Low

64

Real-time

128

High

256

Below Normal

16384

Above Normal

32768

After creating the constant the script connects to the WMI service, then uses this line of code to retrieve a collection of all the processes with the name Notepad.exe:

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

After the collection has been returned we simply use a For Each loop to cycle through all the instances of Notepad.exe. (For this script we’re assuming there’s either only one such instance or, if there all multiple instances, you want all of them to have a Below Normal priority.) Inside that loop we call the SetPriority method, passing as the sole parameter the constant BELOW_NORMAL:

objProcess.SetPriority(BELOW_NORMAL)

If you check the priority of Notepad.exe (either using a script or using Task Manager) you’ll see that it now has a priority of Below Normal.

One caution here: you might want to experiment with different priorities on a test machine before actually trying this script on, say, your file server. Changing the priority of Notepad to Below Normal is probably harmless; changing the priority of your database application to Below Normal might cause problems for your users. Likewise we wouldn’t encourage you to set the process of any priority to Realtime; if you do, it’s very possible that process could gobble up all the system resources and pretty much prevent anything else - including key operating system functions - from running. Just a helpful tip from your friendly neighborhood Scripting Guys!

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment