locked
Stopping chrome when using * RAM RRS feed

  • Question

  • Hello, 

    I'm a novice at powershell and trying to write a script that scans every 10 minutes ( ill be using Task Scheduler for that part ) to see if a broswer is using more than a % of RAM or CPU and then prompt to close the chrome process that is using too much.

    Here's what i have so far which fails due to not under standing i want to close the chrome process greater than 150Mb 
    If (get-process -Name chrome | Where-Object -ge WS 150Mb)
    { 
    $msgBoxInput =  [System.Windows.MessageBox]::Show("Chrome is using too much system resources`nDo you want to close Chrome",'System Resource Manager','YesNo','Error')
    
      switch  ($msgBoxInput) {
    
       ## Close Chrome process using more than 150Mb
      'Yes' { Stop-Process -name chrome -ge WS 150Mb
    
      }
       ## Close 
      'No' {Start-Sleep -Seconds 5}
    
      }
    }
    else {Start-Sleep -Seconds 5}


    • Edited by QFrozenaceQ Thursday, February 15, 2018 1:03 PM
    Thursday, February 15, 2018 1:02 PM

All replies

  • while(1){
        $p = get-process -Name chrome
        if($p.WS -gt 150Mb){
            if('Yes' -eq [System.Windows.MessageBox]::Show("Chrome is using too much system resources`nDo you want to close Chrome",'System Resource Manager','YesNo','Error')){
                Stop-Process -name chrome
            }
        }
        Start-Sleep -Seconds 5
    }
    

    Learn PowerShell  



    \_(ツ)_/

    • Proposed as answer by BOfH-666 Thursday, February 15, 2018 1:22 PM
    Thursday, February 15, 2018 1:19 PM
  • Thanks for the quick reply ! 

    I want it so it only stops the process over 150Mb not all chrome process if possible ?
    Thursday, February 15, 2018 1:41 PM
  • That is what the code says.


    \_(ツ)_/

    Thursday, February 15, 2018 1:46 PM
  • Its currently closing all 
    Thursday, February 15, 2018 1:56 PM
  • Closing all what?  You have to use good English so we can understand what you are trying to say.


    \_(ツ)_/

    Thursday, February 15, 2018 2:14 PM
  • Your current script when run and a Chrome process is above 150Mb it doesn't close that single process over 150Mb it closes all Chrome process. I've been able to do that i need the script to close that one process that is over the 150Mb 
    Thursday, February 15, 2018 2:18 PM
  • Your original code assumed on process.

    Just enumerate the processes in the loop and skip the ones that are not over the limit.


    \_(ツ)_/

    Thursday, February 15, 2018 2:20 PM
  • Sorry if my code was confusing.

    Im trying to do what you said but i dont understand how to.
    Thursday, February 15, 2018 2:26 PM
  • That is why I posted this: Learn PowerShell  

    We don't write custom code on request.


    \_(ツ)_/

    Thursday, February 15, 2018 2:28 PM
  • Hi,

    You can stop the process by ID:
    while(1){
        if($p = Get-Process -Name chrome | Where-Object {$_.WS -gt 150MB}) {
            if('Yes' -eq [System.Windows.MessageBox]::Show("Chrome is using too much system resources`nDo you want to close Chrome",'System Resource Manager','YesNo','Error')){
                Stop-Process -Id $p.Id
            }
        }
        Start-Sleep -Seconds 5
    }

    If you need further help, please feel free to let us know.

    Best Regards,
    Albert

    Please remember to mark the replies as an answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Friday, February 16, 2018 8:23 AM