Categories

Win32_Process examples–set priority

Changing the priority of a process can give a processing boost to an application – but its not always a safe option.

You can modify the process like this:

function set-procpriority {
[CmdletBinding()]
param (
[string]$computername = $env:COMPUTERNAME,
[string]$processname,

[ValidateSet("Idle", "BelowNormal", "Normal", "AboveNormal", "HighPriority", "RealTime")]
[string]$priority
)

switch ($priority){
"Idle"         {[uint32]$priorityin =    64; break}
"BelowNormal"  {[uint32]$priorityin = 16384; break}
"Normal"       {[uint32]$priorityin =    32; break}
"AboveNormal"  {[uint32]$priorityin = 32768; break}
"HighPriority" {[uint32]$priorityin =   128; break}
"RealTime"     {[uint32]$priorityin =   256; break}
}

Get-CimInstance -ClassName Win32_Process -ComputerName $computername -Filter "Name = '$processname'" |
Invoke-CimMethod -MethodName SetPriority -Arguments @{Priority = $priorityin}
}

The advanced takes three parameters – computername (defaults to local machine), a process name and the priority that process should have.

A switch statement converts the priority name to an unsigned integer value

The process objects are retrieved by Get-CimInstance and Invoke-CimMethod is used to call the SetPriority method.

You can use it like this:

£> notepad

£> get-process notepad | Format-List Name, PriorityClass

Name          : notepad
PriorityClass : Normal

£> set-procpriority -processname 'notepad.exe' -priority HighPriority

                                              ReturnValue PSComputerName                                         
                                              ----------- --------------                                          
                                                           0 RSSURFACEPRO2                                          

£> get-process notepad | Format-List Name, PriorityClass

Name          : notepad
PriorityClass : High

The function will modify the priority of all instances of the process. If you want to modify just one instance then you need to remove the process name parameter and add a process id parameter

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>