::::::::: PowerShell :::::::::
Sunday, March 26, 2006
  [MSH] How to change a Process's Priority
There are a lot of posts about how to get processes' name and priorities but I haven't been able to find ways to change a process's priority easily.

Suppose that you are (un)zipping a large file and would like to change the priority of the operation set to "High" to finish the task quickly. One can simply just fire up a Task Manager and set the priority of the process manually with mouse clicks.

But why do so when there is a way to do it with few keystrokes...

If you want to change the process of zip operation you can simply do something like
#RealTime is generally not recommended...(NOTE: One-liner)
gps -p zip.exe | foreach { $_.PriorityClass = "RealTime" }

where valid values for "PriorityClass" can be retrieved through the following function

But the problem with using "foreach" statement in the next pipe is that, when pipe more than one process into the foreach statement, all of them will have their priority set to "RealTime". I don't like the following solution but just to be on the safe side, you can do the following

# Apply the process priority only to the first input object(NOTE: One-liner)
gps -processname "process_name" | &{ $input.moveNext(); $input.Current.PriorityClass = "RealTime" }

Basically you are setting the priority of the first passed pipe object(represented by $input)

You might as well create a function to change the priority more easily

function set-ProcessPriority { 
param($processName = $(throw "Enter process name"), $priority = "Normal")

get-process -processname $processname | foreach { $_.PriorityClass = $priority }
write-host "`"$($processName)`"'s priority is set to `"$($priority)`""

# When zip is running
MSH> set-ProcessPriority zip "High"
"zip"'s priority is set to "High"

# When zip operation is over,("Normal" is the default priority level)
MSH set-ProcessPriority zip
"zip"'s priority is set to "Normal"

To actually see what the priory level is the for given process just to make sure
MSH> gps zip | select name,priorityclass | ft -auto

Name PriorityClass
---- -------------
zip Normal

By the way, I am sure that there could be easier ways to change process priorities. I would love it if anyone can suggest other ways...

Tags :
The other way you can find out what the value values are is to assign it an clearly wrong value and we'll tell you what the value values are (for ENUMs).

[1952:]MSH> $g = gps
[1952:]MSH> $g[0].priorityclass
[1952:]MSH> $g[0].priorityclass="asdf"
Exception setting "PriorityClass": "Cannot convert "asdf" to "System.Diagnostics.ProcessPriorityClass" due to invalid enumeration values. The possible enumeration values are "Normal, Idle, High, RealTime, BelowNormal, AboveNormal"."
At line:1 char:7
+ $g[0].p <<<< riorityclass="asdf"
BTW: This is probably a good example of where it behoove you to type your parameters because you get a better error message. Put the following in a file and run it and compare the error messages:
function sp1
param ($pn, $priority="Normal")
gps -process $pn |foreach {$_.PriorityClass = $priority}
function sp2
param ($pn, [System.Diagnostics.ProcessPriorityClass]$priority="Normal")
gps -process $pn |foreach {$_.PriorityClass = $priority}
function f1
sp1 msh asdfsd

function f2
sp2 msh asdfsd

write-host "`n`n"

I put that into a file t2.msh and ran it:
[1952:]MSH> .\t2.msh
Exception setting "PriorityClass": "Cannot convert "asdfsd" to "System.Diagnostics.Proces
sPriorityClass" due to invalid enumeration values. The possible enumeration values are "N
ormal, Idle, High, RealTime, BelowNormal, AboveNormal"."
At C:\msh\t2.msh:4 char:34
+ gps -process $pn |foreach {$_.P <<<< riorityClass = $priority}

sp2 : Cannot convert "asdfsd" to "System.Diagnostics.ProcessPriorityClass" due to invalid
enumeration values. The possible enumeration values are "Normal, Idle, High, RealTime, B
elowNormal, AboveNormal".
At C:\msh\t2.msh:18 char:6
+ sp2 <<<< msh asdfsd
Notice that when you type the parameter - you'll get an error at the call site instead of in the function. This also helps avoid those situations where you might have done a step in the function that has a side-effect before you encounter the error.

As a general rule, if you are going to fail, it is best to fail as early as possible. Typing can help with that.
Jeffrey Snover
Thanks for the feedback, jeff.
I am glad that you have mentioned that it's better not to throw an error when a process name is not given in this case. I have been wondering when and when not to throw for a null argument.

"an error at the call site instead of in the function"
I have always been a fan of handling as much errors as possible in a function. But letting the caller to output the error message doesn't sound so bad either.
Thanks again for the tips~ :)
Post a Comment

Links to this post:

Create a Link

<< Home
Let's get lazy with PowerShell!

Location: Flushing, NY, United States

Experimenting with a different format of blogs...

10/01/2005 - 11/01/2005 / 11/01/2005 - 12/01/2005 / 12/01/2005 - 01/01/2006 / 01/01/2006 - 02/01/2006 / 02/01/2006 - 03/01/2006 / 03/01/2006 - 04/01/2006 / 04/01/2006 - 05/01/2006 / 05/01/2006 - 06/01/2006 / 06/01/2006 - 07/01/2006 / 07/01/2006 - 08/01/2006 / 08/01/2006 - 09/01/2006 / 10/01/2006 - 11/01/2006 / 11/01/2006 - 12/01/2006 /

Powered by Blogger