{"id":69803,"date":"2005-05-16T11:24:00","date_gmt":"2005-05-16T11:24:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/05\/16\/how-can-i-lower-the-priority-of-a-process-using-a-script\/"},"modified":"2005-05-16T11:24:00","modified_gmt":"2005-05-16T11:24:00","slug":"how-can-i-lower-the-priority-of-a-process-using-a-script","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-lower-the-priority-of-a-process-using-a-script\/","title":{"rendered":"How Can I Lower the Priority of a Process Using a Script?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hey, Scripting Guy! How can I lower the priority of a process using a script?<BR><BR>&#8212; HS<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>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.<\/P>\n<P>Generally speaking most processes run with a <B>Normal<\/B> 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 <I>decrease<\/I> the priority; that way the process won\u2019t interfere with other tasks being carried out on the computer. That\u2019s a quick overview and there are obviously other things to consider before you start changing process priorities. But at least you know what we\u2019re talking about.<\/P>\n<P>So let\u2019s assume that you <I>do<\/I> want to change the priority of a process. You can do that quite easily using Task Manager; just right-click the process, choose <B>Set Priority<\/B>, and then set one of the available options:<\/P><IMG border=\"0\" alt=\"Process Priority\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/priority.jpg\" width=\"350\" height=\"439\"> \n<P><BR>Nice. But can you do this same thing using a script?<\/P>\n<P>Well, as long as you\u2019re running Windows XP or Windows Server 2003 the answer is yes. (Sorry, Windows 2000 users, but this won\u2019t work for you.) That\u2019s because the WMI class <B>Win32_Process<\/B>, found in those versions of Windows, includes a method named <B>SetPriority<\/B>, which allows you to modify the priority of a running process. For example, here\u2019s a script that sets the priority of Notepad.exe to Below Normal:<\/P><PRE class=\"codeSample\">Const BELOW_NORMAL = 16384<\/p>\n<p>strComputer = &#8220;.&#8221;\nSet objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colProcesses = objWMIService.ExecQuery _\n    (&#8220;Select * from Win32_Process Where Name = &#8216;Notepad.exe'&#8221;)\nFor Each objProcess in colProcesses\n    objProcess.SetPriority(BELOW_NORMAL) \nNext\n<\/PRE>\n<P>The script begins by defining a constant named BELOW_NORMAL and setting the value to 16384; as you might expect, we\u2019ll use this constant later on to change the priority of Notepad to Below Normal. Are there other values you can use to assign a <I>different<\/I> priority to a process? Of course there are:<\/P>\n<TABLE id=\"E6D\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Priority Class<\/B><\/P><\/TD>\n<TD>\n<P class=\"lastInCell\"><B>Value<\/B><\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">Normal<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">32<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">Low<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">64<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">Real-time<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">128<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">High<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">256<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">Below Normal<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">16384<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">Above Normal<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">32768<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>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:<\/P><PRE class=\"codeSample\">Set colProcesses = objWMIService.ExecQuery _\n    (&#8220;Select * from Win32_Process Where Name = &#8216;Notepad.exe'&#8221;)\n<\/PRE>\n<P>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\u2019re assuming there\u2019s 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:<\/P><PRE class=\"codeSample\">objProcess.SetPriority(BELOW_NORMAL)\n<\/PRE>\n<P>If you check the priority of Notepad.exe (either using a script or using Task Manager) you\u2019ll see that it now has a priority of Below Normal. <\/P>\n<P>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\u2019t encourage you to set the process of any priority to Realtime; if you do, it\u2019s very possible that process could gobble up all the system resources and pretty much prevent anything else &#8211; including key operating system functions &#8211; from running. Just a helpful tip from your friendly neighborhood Scripting Guys!<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I lower the priority of a process using a script?&#8212; HS 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 [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[31,87,3,5],"class_list":["post-69803","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-operating-system","tag-processes","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I lower the priority of a process using a script?&#8212; HS 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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69803","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=69803"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69803\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=69803"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=69803"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=69803"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}