none
Need advice: Powershell closing tabs for Chrome browsers. RRS feed

  • Question

  • Hello, today i got a request to close particular google chrome browser tabs which are using to much CPU via powershell.

    In the first minute i thought that is an easy one task. For example:

    function Get-Chrome{
    Write-Host ("Hello " + $env:USERNAME)
    sleep 2
    Write-host -ForegroundColor Green  "Letst start Google Chrome and few sites"  
    Start-Process "chrome.exe" "www.google.com"
    Start-Process "chrome.exe"  "www.scorpion3d.com"
    Start-Process "chrome.exe" "www.stackoverflow.com"
    sleep 2
    Write-host -ForegroundColor Green  "Getting CPU load on Chrome browser"  
    sleep 2
    $a = Get-Process -Name chrome -IncludeUserName | Sort-Object -Property CPU -Descending | select -First 1
    Write-host -ForegroundColor Yellow  "Malware hitting the CPU in most cases first. So we need to check which chrome (TAB) process using most of the CPU"  
    sleep 2
    $a
    sleep 2
    Write-host -ForegroundColor Yellow  "Now we know that this user: " $a.username "with pid ID:" $a.Id "using most of the CPU"  
    sleep 2
    Stop-Process -Id $a.Id -Confirm -PassThru
    }

    Problem is that it kills all chrome tabs as it kill all chrome. It does not matter what i am using the particular pid. If you have an idea how to achieve it please let me know.

    • Moved by Bill_Stewart Monday, March 12, 2018 9:08 PM This is not a support forum for third-party software
    Tuesday, January 30, 2018 8:55 PM

All replies

  • PowerShell cannot close individual Windows (tabs) in Chrome.  Post in a Chrome" forum to find out if this is possible via any tools.  I do not believe it can be done with any tool.


    \_(ツ)_/

    Tuesday, January 30, 2018 8:59 PM
  • Hello, thank you for your advice, I will ask them.
    Tuesday, January 30, 2018 9:25 PM
  • Tuesday, January 30, 2018 9:32 PM
  • You can also use this: https://archive.codeplex.com/?p=sepsx to gain automation access to Chrome.

     


    \_(ツ)_/

    • Proposed as answer by SBVRaja Tuesday, March 17, 2020 1:06 AM
    • Unproposed as answer by SBVRaja Tuesday, March 17, 2020 1:06 AM
    Tuesday, January 30, 2018 9:38 PM
  • If you hit CTRL+W, it will close current tab of the browser.

    So, if you pass the same from powershell using below commands it will close the current active tab

    $wshell = New-Object -ComObject wscript.shell

    $wshell.SendKeys("^w")

    But above commands will close the tab of whatever the application that is active while executing the command (it may be IE/Chome or anything else). If you can't be sure on that then you have to use additional command as below by writing code to get the title of the application which you need to keep active.

    $wshell.AppActivate($title)

    Tuesday, March 17, 2020 1:11 AM