xewacox
xewacox

Reputation: 45

PowerShell script to check which browsers are running

I would like to PowerShell script to check the open browsers on the computer, for this I was thinking to use "Get-Process" so it looks like that

if (Get-Process iexplore) { $iexplore = "Internet Explorer; " } else { $iexplore = "Internet Explorer is not running" }
if (Get-Process MicrosoftEdge) { $MicrosoftEdge = "Microsft Edge; " } else { $MicrosoftEdge = "Microsft Edge is not running" }
if (Get-Process msedge) { $msedge = "Microsft Edge(Chromium based); " } else { $msedge = "Microsft Edge Chromium based is not running" }
if (Get-Process chrome) { $chrome = "Google Chrome; " } else { $chrome = "Google Chrome is not running" }
if (Get-Process firefox) { $firefox = "Firefox; " } else { $firefox = "Firefox is not running" }

However, it is working until we do not consider Microsoft browsers as the process may still be in the background even if the browser is not opened. Is there a way to make sure that the result is correct and it is actually checking if the specific browser is opened?

Upvotes: 1

Views: 6475

Answers (2)

swbbl
swbbl

Reputation: 874

Simply use "MainWindowHandle" as filter.

Get-Process -Name '*edge*' | Where-Object { $_.MainWindowHandle -gt 0 }

Upvotes: 1

Gerrit Geeraerts
Gerrit Geeraerts

Reputation: 1184

Depending on your system you could get different results. But if you follow similar approach you could get the same result:

Edge is open:

Get-Process | ? {$_.ProcessName -like "*Edge*"}

outputs:

Handles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName                   
-------  ------    -----      -----     ------     --  -- -----------                   
   1129      58    27444      89468       1,06  13408   2 MicrosoftEdge                 
   1124     109   127456     170436       2,47   2376   2 MicrosoftEdgeCP               
    504      22     6032      27156       0,13   8224   2 MicrosoftEdgeCP               
    501      21     6048      27076       0,08   9368   2 MicrosoftEdgeCP               
   1153     114   155548     208292       3,81  12084   2 MicrosoftEdgeCP               
    292      15     5280      16952       1,17  10232   2 MicrosoftEdgeSH  

Edge is closed:

Get-Process | ? {$_.ProcessName -like "*Edge*"}

outputs:

Handles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName                   
-------  ------    -----      -----     ------     --  -- -----------                   
    825      46    21544      69512       0,39   8976   2 MicrosoftEdge                 
    324      16     4636      19852       0,13   2388   2 MicrosoftEdgeCP               
    265      14     4000      14340       0,05   8924   2 MicrosoftEdgeSH   

solution:

if ((Get-Process MicrosoftEdgeCP -ErrorAction SilentlyContinue | Measure-Object).Count -gt 1){write-host "edge is open"}

Upvotes: 0

Related Questions