{"id":56363,"date":"2008-01-23T03:41:00","date_gmt":"2008-01-23T03:41:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2008\/01\/23\/hey-scripting-guy-how-can-i-change-the-priority-of-each-instance-of-an-application\/"},"modified":"2008-01-23T03:41:00","modified_gmt":"2008-01-23T03:41:00","slug":"hey-scripting-guy-how-can-i-change-the-priority-of-each-instance-of-an-application","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-change-the-priority-of-each-instance-of-an-application\/","title":{"rendered":"Hey, Scripting Guy! How Can I Change the Priority of Each Instance of an Application?"},"content":{"rendered":"<h2><img decoding=\"async\" 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\" \/> <\/h2>\n<p>Hey, Scripting Guy! Is there any way to permanently change the priority of a process? If there isn\u2019t, how can I write a script that will monitor for new instances of this process and then change the priority each time the application runs?<\/p>\n<p>&#8212; AM<\/p>\n<p><img decoding=\"async\" border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\" \/><img decoding=\"async\" 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 decoding=\"async\" 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> <\/p>\n<p>Hey, AM. You know, <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/jan08\/hey0122.mspx\"><b>just yesterday<\/b><\/a> we noted that <i>Hey, Scripting Guy!<\/i> is <i>the<\/i><i><\/i>place<i><\/i>to come if you\u2019re looking for the inside scoop on what\u2019s going on in the world of system administration scripting. To prove it, in yesterday\u2019s column we told you about three new challenges that have been added to the <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/funzone\/games\/default.mspx\"><b>2008 Winter Scripting Games<\/b><\/a>. If that\u2019s not a scoop, well, we\u2019d like to know what is.<\/p>\n<table id=\"EWD\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<thead><\/thead>\n<tbody>\n<tr class=\"record\" vAlign=\"top\">\n<td>\n<p><b>Note<\/b>. According to <a href=\"http:\/\/www.m-w.com\/dictionary\/scoop\" target=\"_blank\"><b>Merriam-Webster\u2019s online dictionary<\/b><\/a> a scoop is \u201csmall spoon-shaped utensil or instrument for cutting or gouging.\u201d So there!<\/p>\n<p>Oh, wait; that\u2019s the wrong definition. Try this one instead: \u201cinformation especially of immediate interest.\u201d So <i>there<\/i>!<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>Of course, we realize that many of you are still a little skeptical. \u201cOK, so you had one scoop,\u201d you\u2019re thinking. \u201cWhat do you want, a Pulitzer Prize for journalism?\u201d<\/p>\n<p>Well, now that you mention it, yes, we <i>do<\/i> want a Pulitzer prize for journalism. (Now that we\u2019ve won a <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/tnmag\/archive.mspx\"><b>TABBIE Award<\/b><\/a>, well, the Pulitzer is about the only award we <i>haven\u2019t<\/i> won.) That\u2019s why we decided to win you skeptics over by presenting you with yet another scoop: in addition to all the other <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/funzone\/games\/games08\/prizes.mspx\"><b>great prizes<\/b><\/a> being handed out during the upcoming Scripting Games we\u2019re going to be giving away 50 copies of Windows Vista Ultimate!<\/p>\n<p>That\u2019s right, Windows Vista Ultimate, the, well, ultimate operating system. (Don\u2019t believe us? Then count the number of checkmarks on <a href=\"http:\/\/www.microsoft.com\/windows\/products\/windowsvista\/editions\/choose.mspx\" target=\"_blank\"><b>this page<\/b><\/a>. What did we tell you?) Windows Vista Ultimate is pretty darn cool, but it\u2019s also a little pricey; the suggested retail price for a single copy is $958,000. That\u2019s a lot of money for an operating system.<\/p>\n<p>Well, OK, the suggested retail price is actually $399; for many people, however, it might as well be $958,000. But that\u2019s life, right? After all, Windows Vista Ultimate is packed with cool new features like Windows Media Center and BitLocker Drive Encryption, and cool new features like that don\u2019t come free.<\/p>\n<p><i>Unless<\/i> you take part in the 2008 Winter Scripting Games (February 15<sup>th<\/sup> through March 3<sup>rd<\/sup>, right here in the Script Center). Enter a single event in the Scripting Games, and you\u2019ll be eligible to win a free copy of Windows Vista Ultimate. Heck, enter the Games and you <i>will<\/i> win a free copy of Windows Vista Ultimate!<\/p>\n<p>Just a second \u2026. Um, OK, if you say so.<\/p>\n<p>The Scripting Editor has suggested that we change that statement to the following:<\/p>\n<p>Enter the Games and you <i>might<\/i> win a free copy of Windows Vista Ultimate.<\/p>\n<p>As you wish, Scripting Editor. <\/p>\n<table id=\"E6F\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<thead><\/thead>\n<tbody>\n<tr class=\"record\" vAlign=\"top\">\n<td>\n<p><b>Note<\/b>. Personally we still think that you <i>will<\/i> win a copy. But we have to go along with what the Scripting Editor says. After all, she\u2019s big (way bigger than any of us), and she\u2019s mean.<\/p>\n<p><b>Ed<\/b><b>itor\u2019s Note.<\/b> Well, she\u2019s bigger (<i>taller<\/i>, anyway) than the Scripting Guy who writes this column. But then again, who isn\u2019t?<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>Pretty exciting news, huh? And if that\u2019s not enough here\u2019s another scoop for you: today\u2019s column features a script that can automatically change the priority of any new instances of Notepad that get started on a computer. We couldn\u2019t find a simple, surefire way to permanently change the priority of a process. But this script does the next best thing: it monitors for new instances of Notepad and, any time it finds one, it immediately changes the priority of the process. Notepad will still <i>start<\/i> with Normal priority, but in a matter of seconds that priority will be changed to Below Normal.<\/p>\n<p>Try to find a scoop like <i>that<\/i> in <i>The New York Times<\/i> or <i>The Wall Street Journal<\/i>!<\/p>\n<p>Well, OK, good point. But try to find a scoop like that in <i>The New York Times<\/i>.<\/p>\n<p>Here\u2019s the script: <\/p>\n<pre class=\"codeSample\">Const BELOW_NORMAL = 16384\n\nstrComputer = \".\"\n\nSet objWMIService = GetObject(\"winmgmts:\\\\\" &amp; strComputer &amp; \"\\root\\cimv2\")\n\nSet colProcesses = objWMIService.ExecNotificationQuery _\n    (\"Select * From __InstanceCreationEvent Within 5 Where TargetInstance ISA 'Win32_Process'\")\n\nDo While True\n    Set objLatestProcess = colProcesses.NextEvent\n    If objLatestProcess.TargetInstance.Name = \"notepad.exe\" Then\n        objLatestProcess.TargetInstance.SetPriority(BELOW_NORMAL)\n    End If\nLoop\n<\/pre>\n<p>So how does this script actually work? Well, to begin with, we define a constant named BELOW_NORMAL and set the value to 16384. (Which, by amazing coincidence, just happens to be the exact number of things the Scripting Guy who writes this column still has to finish in order to be ready for the Scripting Games.) This constant enables us to set the priority for our process to Below Normal. Would you prefer to set the priority to something other than Below Normal? Then take a peek at the <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/guide\/sas_prc_dxpz.mspx\"><b>Microsoft Windows 2000 Scripting Guide<\/b><\/a> for information on the other available priorities and the required constant values.<\/p>\n<p>After defining the constant we connect to the WMI service on the local computer. (And sure, we could use this same script to monitor and change process priorities on a remote computer. All we have to do is assign the name of that computer to the variable strComputer.) Once we\u2019re connected to the WMI service we can then use the following query to create an event subscription that notifies us any time a new process is created:<\/p>\n<pre class=\"codeSample\">Set colProcesses = objWMIService.ExecNotificationQuery _\n    (\"Select * From __InstanceCreationEvent Within 3 Where TargetInstance ISA 'Win32_Process'\")\n<\/pre>\n<p>We won\u2019t discuss the details of the <b>ExecNotificationQuery<\/b> method in today\u2019s column; if you\u2019d like to know more about what this query does \u2013 and how it does it \u2013 you might want to take a look at the Scripting Guys webcast <a href=\"http:\/\/msevents.microsoft.com\/cui\/eventdetail.aspx?EventID=1032268754&amp;culture=en-US\" target=\"_blank\"><b>An Ounce of Prevention: An Introduction to WMI Events<\/b><\/a>. For now, suffice to say that every 3 seconds (Within 3) the query checks the __InstanceCreationEvent class (a class that keeps track of new instances of all WMI objects). If any new processes have been created (TargetInstance ISA &#8216;Win32_Process&#8217;) our script will be notified of that fact. If no new processes have been created everything will pause for 3 seconds; when those 3 seconds are up ExecNotificationQuery will check the __InstanceCreationEvent class again. And again. And again. Ad infinitum.<\/p>\n<p>After creating the event subscription query we set up a Do loop designed to run forever. (Which, by the way, is one good reason to run this script in a command window under CScript; that way you can terminate the script simply by closing the command window.) Inside the loop the first thing we do is execute the following line of code:<\/p>\n<pre class=\"codeSample\">Set objLatestProcess = colProcesses.NextEvent\n<\/pre>\n<p>This line causes the script to \u201cblock\u201d until it receives a notice telling it that a new process has been created; that simply means that the script is going to pause on this line of code and not budge, at least not until a new process has been created. If and when a new process <i>is<\/i> created, we check to see if the process <b>Name<\/b> (or, more correctly, the Name of the TargetInstance, the new member of the __InstanceCreationEvent class) is equal to <i>notepad.exe<\/i>:<\/p>\n<pre class=\"codeSample\">If objLatestProcess.TargetInstance.Name = \"notepad.exe\" Then\n<\/pre>\n<p>If the Name <i>isn\u2019t<\/i> equal to <i>notepad.exe<\/i> then we simply zip back to the top of the loop and wait for the next event to occur. (That is, the next process to be created.) If the Name <i>is<\/i> equal to <i>notepad.exe<\/i> then we use this line of code to set the priority for this new instance of Notepad to Below Normal:<\/p>\n<pre class=\"codeSample\">objLatestProcess.TargetInstance.SetPriority(BELOW_NORMAL)\n<\/pre>\n<p>And then, once more, it\u2019s back to the top of the loop where we wait for the next process to be created. Each time an instance of Notepad is created the priority will be reset to Below Normal, something we can verify by looking at Task Manager:<\/p>\n<p><img decoding=\"async\" border=\"0\" alt=\"Spacer\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/prioritytm.jpg\" width=\"489\" height=\"218\" \/> <\/p>\n<p>Cool, huh? When\u2019s the last time you saw a picture like <i>that<\/i> in <i>The New York Times<\/i>?<\/p>\n<p>Really? Guess we must not have seen the paper that day.<\/p>\n<p>That should do it, AM; as long as this script is running any new instance of Notepad that starts on the computer will have its priority automatically switched to Below Normal (more commonly-referred to as \u201cScripting Guys mode\u201d). It\u2019s not the same thing as permanently changing the process priority, but it\u2019s not bad.<\/p>\n<p>Before you all go, don\u2019t forget that the Scripting Games start <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/funzone\/games\/default.mspx\"><b>right here<\/b><\/a> on Friday, February 15<sup>th<\/sup>. We\u2019ve got fun, we\u2019ve got prizes, we\u2019ve got scripting competitions; what more could you ask for?<\/p>\n<p>Good point: food <i>would<\/i> be nice, wouldn\u2019t it? Tell you what, enter the Scripting Games and the Scripting Guys <i>will<\/i> send pizza and beer to everyone! On the house!<\/p>\n<p>Um, sorry. Apparently that sentence should have read:<\/p>\n<p>Enter the Scripting Games and the Scripting Guys <i>won\u2019t<\/i> send pizza and beer to everyone!<\/p>\n<p>We apologize for any misunderstanding. <\/p>\n<table id=\"EJCAC\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<thead><\/thead>\n<tbody>\n<tr class=\"record\" vAlign=\"top\">\n<td>\n<p><b>Note<\/b>. Just between us, though, we\u2019ll see what we can do. Pepperoni pizza OK with everyone?<\/p>\n<p><b>Ed<\/b><b>itor\u2019s Note.<\/b> No, we\u2019re not sending pizza and\/or beer to anyone. Sorry. (The part earlier that said the Scripting Editor is mean was completely accurate.)<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! Is there any way to permanently change the priority of a process? If there isn\u2019t, how can I write a script that will monitor for new instances of this process and then change the priority each time the application runs? &#8212; AM Hey, AM. You know, just yesterday we noted that Hey, [&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-56363","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! Is there any way to permanently change the priority of a process? If there isn\u2019t, how can I write a script that will monitor for new instances of this process and then change the priority each time the application runs? &#8212; AM Hey, AM. You know, just yesterday we noted that Hey, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/56363","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=56363"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/56363\/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=56363"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=56363"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=56363"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}