Quantcast
Channel: ::::::::: PowerShell :::::::::
Viewing all articles
Browse latest Browse all 25

[MSH] How to change a Process's Priority

$
0
0
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
MSH>[Enum]::getNames("Diagnostics.ProcessPriorityClass")
Normal
Idle
High
RealTime
BelowNormal
AboveNormal


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 :

Viewing all articles
Browse latest Browse all 25

Trending Articles