PowerShell get URL of Browser tab

Viewed 1854

I am working on a script that serves as a support ticket creator using PowerShell. When activated at any random time in the context of any user, the script shall look for open browser tabs in msedge, firefox, and IE, and grab the focused windows' URLs if they are open.

I looked through this forum and many others, found many negative responses and one link talking about a solution but the download link to the script does not work anymore.

That is why I wanted to ask: is there any solution to this? My script already grabs the Window Title properties:

$msedgetitle = Get-Process MSEdge | select -expandproperty MainWindowTitle -ErrorAction SilentlyContinue
$chrometitle = Get-Process chrome | select -expandproperty MainWindowTitle -ErrorAction SilentlyContinue
$iexploretitle = Get-Process iexplore | select -expandproperty MainWindowTitle -ErrorAction SilentlyContinue

Many thanks in advance AÖ

2 Answers

This code will return the url of the active tab in Chrome. Note the timing is crucial and will need to be experimented on using your system.

function Show-Process($Process, [Switch]$Maximize) {

<#
  Function Courtsy of:
  community.idera.com/database-tools/powershell/powertips/b/
  tips/posts/bringing-window-in-the-foreground
#>

  $sig = '
    [DllImport("user32.dll")] public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
    [DllImport("user32.dll")] public static extern int SetForegroundWindow(IntPtr hwnd);
  '
  
  if ($Maximize) { $Mode = 3 } else { $Mode = 4 }
  $type = Add-Type -MemberDefinition $sig -Name WindowAPI -PassThru
  $hwnd = $process.MainWindowHandle
  $null = $type::ShowWindowAsync($hwnd, $Mode)
  $null = $type::SetForegroundWindow($hwnd) 
}

Clear-Host
Set-Clipboard -Value $null
$wshell=New-Object -ComObject wscript.shell
$Null = $wshell.AppActivate('Chrome') # Activate on Chrome browser
Sleep 5 # Interval (in seconds) between switch 
$wshell.SendKeys("{F6}") # F6 Select Address Bar
Sleep 1 # Interval (in seconds) between switch 
$wshell.SendKeys("^c")
Sleep 1 # Interval (in seconds) between switch 
$MyURL = Get-Clipboard
"$MyURL"
$PSId = (Get-process -name "PowerShell*").ID
Show-Process -Process (Get-Process -Id $PSId) -Maximize
Remove-Variable wshell

Results:

https://stackoverflow.com/questions/67297306/powershell-get-url-of-browser-tab
PS> 

HTH

Added to RetiredGeek's great answer.

Copy his code and add this to the end. Using sendkeys is not pretty. If you have multiple chrome windows open you'll need to alt tab yourself with code as is. Split your screen so you can see both PowerShell and chrome window. Let it run through chrome tabs a couple of times and just select -unique to remove dups. Don't do anything else while this runs as sendkeys will send those keys to whatever app is in focus that instant. Once all tabs show in PS console then switch to PS and press ctrl-c. $results will have the URLs from tabs.

Function LoopThruTabs {
Clear-Host
$results=@()
Set-Clipboard -Value $null
$wshell=New-Object -ComObject wscript.shell
$Null = $wshell.AppActivate('Chrome') # Activate on Chrome browser
Sleep 5 # Interval (in seconds) between switch 
While($true){
$wshell.SendKeys("^{TAB}") # TAB Switch chrome tab
Sleep 2 # Interval (in seconds) between switch 
$wshell.SendKeys("{F6}") # F6 Select Address Bar
Sleep 1 # Interval (in seconds) between switch 
$wshell.SendKeys("^c")
Sleep 1 # Interval (in seconds) between switch 
$MyURL = Get-Clipboard
"$MyURL" ; $Global:results +=$MyURL
}
}

LoopThruTabs 
$results | select -Unique
Related