Search code examples
powershellprocessmicrosoft-edge

Trying to Stop process of msedge by window title


I have some questions regarding Microsoft Edge and PowerShell.

When two Edge Windows with different Window Titles are open, I would like to specify one window title in PowerShell and use Stop-Process to close the window with that window title. The ps1 file code with the expected specifications is below.

$TargetTitle = "test"

$EdgeProcesses = (Get-Process -Name msedge)
foreach ($process in $EdgeProcesses) {
    $title = $process.MainWindowTitle
    if ($title -eq $TargetTitle) {
        $id = $process.Id
        Stop-Process -Id $id
    }
}

When windows with titles A and B are open, and when you use the above code to specify the window with the title A and Stop-Process, it will include not only the window with the title A, but also all Edge windows, including the window of title B. The same thing happens even if Edge is opened in app mode.

  1. Why does the above code behave like this?
  2. If you run the above code after activating the window with the last title Otsu in the above, the window will not close in the first place. Why is this.

Solution

  • It seems that Edge only generates one title group which includes all the windows. I think it's related with Edge’s multi-process architecture.

    If you're trying to automate Edge, I suggest that you can use WebDriver.