Search code examples
powershellprocessjobs

How to set low priority for all child processes created by a PS script in the script


I want to set all child processes I start with a Invoke-expression to BelowNormal and I want to do it in the PowerShell script. I have

....
$scr= {
  param($di,$op, $log)
  [System.Threading.Thread]::CurrentThread.Priority ='BelowNormal'
  cd $di
  Invoke-Expression (".\Test.exe -x$op ") | Select-String -Pattern "" -Context 5 | Add-Content $log -Append
}
Sart-Job -ScriptBlock $scr -ArgumentList $d, $X , $fl

But when I look in the Task Manger I can see the Test.exe just have normal priority why? How can I in my script ensure that all my child processes get belowNormal priority?


Solution

  • Updating the thread's .Priority doesn't seem to have any effect in the process priority. Updating the PriorityClass of the job's process seem to solve the problem, testing with cmd I can see that is started with BelowNormal priority:

    $job = Start-Job {
        (Get-Process -Id $PID).PriorityClass = 'BelowNormal'
        cmd /c ping -n 20 google.com
    }