{"id":50673,"date":"2010-04-12T00:01:00","date_gmt":"2010-04-12T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2010\/04\/12\/hey-scripting-guy-can-i-use-windows-powershell-to-lower-the-priority-of-a-process\/"},"modified":"2010-04-12T00:01:00","modified_gmt":"2010-04-12T00:01:00","slug":"hey-scripting-guy-can-i-use-windows-powershell-to-lower-the-priority-of-a-process","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-can-i-use-windows-powershell-to-lower-the-priority-of-a-process\/","title":{"rendered":"Hey, Scripting Guy! Can I Use Windows PowerShell to Lower the Priority of a Process?"},"content":{"rendered":"<p class=\"MsoNormal\"><a class=\"addthis_button\" href=\"http:\/\/www.addthis.com\/bookmark.php?v=250&amp;pub=scriptingguys\"><img decoding=\"async\" alt=\"Bookmark and Share\" src=\"http:\/\/s7.addthis.com\/static\/btn\/v2\/lg-share-en.gif\" width=\"125\" height=\"16\"><\/a>&nbsp;<\/p>\n<p class=\"MsoNormal\">&nbsp;<\/p>\n<p><img decoding=\"async\" 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\"><\/p>\n<p class=\"MsoNormal\">Hey, Scripting Guy! I have a problem with a rather annoying program at work. The program starts up and runs with high priority. When it does this, my computer is slow as a snail, and as unresponsive as a bear hibernating in the winter. I would like to use Windows PowerShell to lower the priority of this process. Can this be done?<\/p>\n<p class=\"MsoNormal\">&#8212; CS<\/p>\n<p class=\"MsoNormal\">\n<p>&nbsp;<\/p>\n<\/p>\n<p class=\"MsoNormal\"><img decoding=\"async\" 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\">Hello CS, Microsoft Scripting Guy Ed Wilson here. Speaking of winter, the Scripting Wife and I are heading to <a href=\"http:\/\/en.wikipedia.org\/wiki\/Raleigh,_North_Carolina\"><font face=\"Segoe\">Raleigh, North Carolina<\/font><\/a>, this weekend to visit some friends, and to see an ice show. It should be pretty cool. (As an aside, I have visited nearly one-quarter of all the countries in the world, but this will be my first visit to the capital city of my home state. So I am psyched.) <\/p>\n<p class=\"MsoNormal\">CS, you mention bears. One thing to keep in mind about hibernating bears in the winter is that not all bears actually hibernate. Polar bears do a sort of <a href=\"http:\/\/en.wikipedia.org\/wiki\/Polar_Bear\"><font face=\"Segoe\">waking hibernation<\/font><\/a> that allows them to conserve food, but I imagine if you came across one, he would be rather responsive. Just a safety tip as it were&mdash;it might be somewhat interesting if one were to awaken a 1,500-pound (680-kilogram) bear that was snoozing because he was hungry. My favorite polar bear is named <a href=\"http:\/\/en.wikipedia.org\/wiki\/Knut_(polar_bear)\"><font face=\"Segoe\">Knut<\/font><\/a>, and the Scripting Wife and I had the opportunity to meet him in <a href=\"http:\/\/en.wikipedia.org\/wiki\/Berlin\"><font face=\"Segoe\">Berlin, Germany<\/font><\/a>, when I was there teaching a Windows PowerShell class. We went to the <a href=\"http:\/\/en.wikipedia.org\/wiki\/Berlin_Zoo\"><font face=\"Segoe\">Berlin Zoological Garden<\/font><\/a> and spent a lovely day taking pictures and walking around. As you can see from this photo I took, Knut is no longer a little cub, but he is still cute. <\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Photo Ed took in Berlin, Germany, of Knut the polar bear\" alt=\"Photo Ed took in Berlin, Germany, of Knut the polar bear\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/april\/hey0412\/hsg-04-12-10-01.jpg\" width=\"600\" height=\"479\"><\/p>\n<p class=\"Fig-Graphic\">\n<p>&nbsp;<\/p>\n<\/p>\n<p class=\"MsoNormal\">CS, you can use Windows Management Instrumentation (WMI) to work with the priority of a process in Windows PowerShell. To see the priority of a process (such as Notepad) use the <b>Get-WmiObject<\/b> cmdlet to query the <b>Win32_Process<\/b> class. You could use this command (or using the <b>gwmi<\/b> alias and positional arguments, it would be <b>gwmi Win32_process<\/b>), but you would get back several pages of information about every process on your computer:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">Get-WmiObject -Class win32_process<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">To limit the information that is returned to only the Notepad process, you can add a filter to your command (<b>gwmi win32_process -f &#8220;name=&#8217;notepad.exe'&#8221;<\/b> using the alias, positional arguments, and partial parameter completion):<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">Get-WmiObject win32_process -Filter &#8220;name=&#8217;notepad.exe'&#8221;<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">Using only the filter to limit results to the Notepad process may be just ducky for you. Depending on how busy I am, I may simply search through the 60 lines of output and find the <b>priority<\/b> property to see at what priority the process is currently running. This is shown in the following image.<\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of searching through lines of code for priority property\" alt=\"Image of searching through lines of code for priority property\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/april\/hey0412\/hsg-04-12-10-02.jpg\" width=\"600\" height=\"433\"><\/p>\n<p><\/p>\n<p>If I have more time, I may pipe the results to the <b>Format-Table<\/b> cmdlet and choose the properties I am interested in examining. This is illustrated here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">PS C:&gt; gwmi win32_process -f &#8220;name=&#8217;notepad.exe'&#8221; | Format-Table name, priority, Handle -AutoSize<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><\/p>\n<p><font face=\"Lucida Sans Typewriter\">&nbsp;<\/font><\/p>\n<p><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">name<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>priority Handle<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">&#8212;-<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>&#8212;&#8212;&#8211; &#8212;&#8212;<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">notepad.exe<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>6 4236<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><\/p>\n<p><font face=\"Lucida Sans Typewriter\">&nbsp;<\/font><\/p>\n<p><\/span><\/p>\n<p class=\"CodeBlock\"><span><\/p>\n<p><font face=\"Lucida Sans Typewriter\">&nbsp;<\/font><\/p>\n<p><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">PS C:&gt; <\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">At any rate, <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa394372(VS.85).aspx\"><font face=\"Segoe\">MSDN documentation<\/font><\/a> tells us that the <b>priority<\/b> property of the <b>Win32_Process<\/b> WMI class is reported as a range of numbers\n from 0 (lowest) to 31 (highest). <\/p>\n<p class=\"MsoNormal\">To set the priority of a process, use the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa393587(VS.85).aspx\"><font face=\"Segoe\">SetPriority<\/font><\/a> method. The thing that is confusing is that the <b>SetPriority<\/b> method takes an integer to tell it which priority to set; however, it is a different number than the one that you retrieve from the <b>priority<\/b> property. To use this method, you need to refer to the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa393587(VS.85).aspx\"><font face=\"Segoe\">table on MSDN<\/font><\/a> that details the allowed values and their meanings. Table 1 is a summary of those values. <\/p>\n<p class=\"TableNum-Title\"><strong>Table 1<span>&nbsp; <\/span>Values and Meanings of the SetPriority Method<\/p>\n<p><\/strong><\/p>\n<table class=\"MsoNormalTable\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td valign=\"top\" width=\"126\">\n<p class=\"TableHead\"><strong>Value <\/p>\n<p><\/strong><\/p>\n<\/td>\n<td valign=\"top\" width=\"126\">\n<p class=\"TableHead\"><b>Meaning <\/p>\n<p><\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"126\">\n<p class=\"TableText\">64 (0x40) <\/p>\n<\/td>\n<td valign=\"top\" width=\"126\">\n<p class=\"TableText\">Idle <\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"126\">\n<p class=\"TableText\">16384 (0x4000) <\/p>\n<\/td>\n<td valign=\"top\" width=\"126\">\n<p class=\"TableText\">Below Normal <\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"126\">\n<p class=\"TableText\">32 (0x20) <\/p>\n<\/td>\n<td valign=\"top\" width=\"126\">\n<p class=\"TableText\">Normal <\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"126\">\n<p class=\"TableText\">32768 (0x8000)<\/p>\n<\/td>\n<td valign=\"top\" width=\"126\">\n<p class=\"TableText\">Above Normal <\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"126\">\n<p class=\"TableText\">128 (0x80) <\/p>\n<\/td>\n<td valign=\"top\" width=\"126\">\n<p class=\"TableText\">High Priority <\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"126\">\n<p class=\"TableText\">256 (0x100) <\/p>\n<\/td>\n<td valign=\"top\" width=\"126\">\n<p class=\"TableText\">Real Time<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p class=\"MsoNormal\">\n<p>&nbsp;<\/p>\n<\/p>\n<p class=\"MsoNormal\">To change the priority of a process, you need to connect directly to the process. The <b>SetPriority<\/b> method is what is called an <b>instance<\/b> method, and therefore we need to return an instance of a process. To do this, use the <b>key<\/b> property of the class. The <b>key<\/b> property for <b>Win32_Process<\/b> is <b>Handle<\/b> (MSDN reports the key property qualifier). Use the WMI class accelerator to connect to the specific instance of the WMI class. The code to do this is shown here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">[wmi]&#8221;win32_process.handle=&#8217;4236&#8242;&#8221;<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">When that line of code is run, the results appear that are shown in the following image.<\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of what is shown when line of code runs\" alt=\"Image of what is shown when line of code runs\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/april\/hey0412\/hsg-04-12-10-03.jpg\" width=\"600\" height=\"414\"><\/p>\n<p class=\"Fig-Graphic\">\n<p>&nbsp;<\/p>\n<\/p>\n<p class=\"MsoNormal\">Now that I know that the command is returning the exact Notepad process that I am interested in working with, it is time to call the <b>SetPriority<\/b> method. To do this we modify the command just a little bit, as seen here where we change the priority of the Notepad process to a very low priority:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">PS C:&gt; ([wmi]&#8221;win32_process.handle=&#8217;4236&#8242;&#8221;).setPriority(64)<\/p>\n<p>__GENUS<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 2<br \/>__CLASS<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: __PARAMETERS<br \/>__SUPERCLASS<span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>:<br \/>__DYNASTY<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: __PARAMETERS<br \/>__RELPATH<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>&nbsp;&nbsp;<\/span>:<br \/>__PROPERTY_COUNT : 1<br \/>__DERIVATION<span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: {}<br \/>__SERVER<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>:<br \/>__NAMESPACE<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>:<br \/>__PATH<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>:<br \/>ReturnValue<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 0<\/p>\n<p>PS C:&gt;<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">&nbsp;<\/p>\n<p class=\"MsoNormal\">CS, that is all there is to using WMI to change the priority of a process. WMI Week will continue tomorrow when we will talk about&hellip;wait a minute. <\/p>\n<p class=\"MsoNormal\">If you want to know exactly what we will be looking at tomorrow, follow us on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\"><font face=\"Segoe\">Twitter<\/font><\/a> or <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send e-mail to us at <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\" target=\"_blank\"><font face=\"Segoe\">scripter@microsoft.com<\/font><\/a> or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p class=\"MsoNormal\"><span><\/p>\n<p>&nbsp;<\/p>\n<p><\/span><\/p>\n<p><b><span>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/p>\n<p><\/span><\/b><\/p>\n<p><b><span><\/span><\/b>&nbsp;<\/p>\n<p class=\"MsoNormal\"><span><\/p>\n<p>&nbsp;<\/p>\n<p><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; &nbsp; Hey, Scripting Guy! I have a problem with a rather annoying program at work. The program starts up and runs with high priority. When it does this, my computer is slow as a snail, and as unresponsive as a bear hibernating in the winter. I would like to use Windows PowerShell to 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":[87,3,45],"class_list":["post-50673","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-processes","tag-scripting-guy","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>&nbsp; &nbsp; Hey, Scripting Guy! I have a problem with a rather annoying program at work. The program starts up and runs with high priority. When it does this, my computer is slow as a snail, and as unresponsive as a bear hibernating in the winter. I would like to use Windows PowerShell to lower [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/50673","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=50673"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/50673\/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=50673"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=50673"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=50673"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}