Can a webpage be opened in a browser by a PowerShell command, but leave the PowerShell console window as the active window?
When I execute a command like
start http:\\www.google.com
the webpage opens in a browser window, but the active window (the one receiving the next keyboard input) is switched to the browser's window. I would like the active window to continue to be the PowerShell command prompt window.
Is this possible?
August 21st, 2015 11:41pm
Here's one way:
cmd /c start /min http://www.google.com
I tried using -WindowStyle Minimized with Start-Process, but focus kept going to the browser.
August 21st, 2015 11:53pm
Here's one way:
cmd /c start /min http://www.google.com
I tried using -WindowStyle Minimized with Start-Process, but focus kept going to the br
August 22nd, 2015 12:04am
Interesting.
Worked for me with v4 on 8.1 using IE11.
August 22nd, 2015 12:14am
Interesting.
Worked for me with v4 on 8.1 using IE11.
I'll experiment with it on my wife's 8.1 laptop, and also on my Windows 10 tablet.
Thanks for the idea.
Maybe there is a way for a powershell.exe process to try to claim the focus.
And, BTW, I failed to mention that I am using the powershell.exe console.
August 22nd, 2015 12:24am
Here's one way:
cmd /c start /min http://www.google.com
For me, using Windows 7 and Windows 8.1, this works only if the browser is not already running.
If the browser is running, even if minimized, the browser keeps the focus.
For Windows 10, even if the browser is not running it does not work to retain focus on the powershell.exe session.
August 23rd, 2015 5:02am
Hi Larry,
could you try this:
$url = "www.google.com"
$ie = New-Object -Com InternetExplorer.Application
$ie.Navigate2($url)
$ie.Visible = $True
$ie.Fullscreen = $True
This should open an IE browser with google as webpage, but keeps the focus on your PowerShell console.
August 24th, 2015 6:11am
HicanNL, thanks, but that code also switched the focus from powershell.exe to IE session.
August 24th, 2015 6:58am
And if you do it like this? On my lab machine the console doesn't lose focus:
$url = "www.google.com"
$ie = New-Object -Com InternetExplorer.Application
$ie.Navigate2($url)
$ie.Visible = $True
$ie.Fullscreen = $True
Add-Type -Assembly "Microsoft.VisualBasic"
[Microsoft.VisualBasic.Interaction]::AppActivate($pid)
-
Marked as answer by
LarryWeiss
16 hours 51 minutes ago
August 24th, 2015 9:02am
Thanks! The code below works for me. Note that I changed the value of $ie.Fullscreen to $False. With $True, the IE window opened without stealing focus, but there was no frame around it.
$url = "www.google.com"
$ie = New-Object -Com InternetExplorer.Application
$ie.Navigate2($url)
$ie.Visible = $True
$ie.Fullscreen = $False
Add-Type -Assembly "Microsoft.VisualBasic"
[Microsoft.VisualBasic.Interaction]::AppActivate($pid)
-
Marked as answer by
LarryWeiss
3 hours 53 minutes ago
August 24th, 2015 10:19am
And if you do it like this? On my lab machine the console doesn't lose focus:
$url = "www.google.com"
$ie = New-Object -Com InternetExplorer.Application
$ie.Navigate2($url)
$ie.Visible = $True
$ie.Fullscreen = $True
Add-Type -Assembly "Microsoft.VisualBasic"
[Microsoft.VisualBasic.Interaction]::AppActivate($pid)
-
Marked as answer by
LarryWeiss
Monday, August 24, 2015 2:18 PM
August 24th, 2015 1:00pm
Thanks! The code below works for me. Note that I changed the value of $ie.Fullscreen to $False. With $True, the IE window opened without stealing focus, but there was no frame around it.
$url = "www.google.com"
$ie = New-Object -Com InternetExplorer.Application
$ie.Navigate2($url)
$ie.Visible = $True
$ie.Fullscreen = $False
Add-Type -Assembly "Microsoft.VisualBasic"
[Microsoft.VisualBasic.Interaction]::AppActivate($pid)
-
Marked as answer by
LarryWeiss
Tuesday, August 25, 2015 3:16 AM
August 24th, 2015 2:17pm
What do the extra commands do? Specifically:
Add-Type -Assembly
"Microsoft.VisualBasic"
[Microsoft.VisualBasic.Interaction]::AppActivate($pid)
August 24th, 2015 2:53pm
What do the extra commands do? Specifically:
Add-Type -Assembly
"Microsoft.VisualBasic"
[Microsoft.VisualBasic.Interaction]::AppActivate($pid)
$pid stores the PID of your console session. The AppActivate method is being used to force focus back to the console.
https://msdn.microsoft.com/en-us/library/dyz95fhy(v=vs.90).aspx
I'm not sure if this will always be reliable, but it seems to work in this instance.
August 24th, 2015 2:57pm
What do the extra commands do? Specifically:
Add-Type -Assembly
"Microsoft.VisualBasic"
[Microsoft.VisualBasic.Interaction]::AppActivate($pid)
$pid stores the PID of your console session. The AppActivate method is being used to force focus back to the console.
https://msdn.microsoft.com/en-us/library/dyz95fhy(v=vs.90).aspx
I'm not sure if this will always be reliable, but it seems to work in this instance.
I need to test this also on Windows 8.1 and 10, but it works well enough on this PC running Windows 7 that I'll be able to use it during a demo I'm doing next month.
August 24th, 2015 3:34pm
And the code below works fine too (at least with with POSH 3 and Windows 7). It allows you to invoke the browser currently associated with URL links. I wish there was something more rigorous I could code for that sleep 2 delay
that allows the browser to establish its window before the powershell.exe process reclaims the focus.
$url = 'www.google.com'
Invoke-Expression "start $url"
sleep 2
Add-Type -Assembly 'Microsoft.VisualBasic'
[Microsoft.VisualBasic.Interaction]::AppActivate($pid)
-
Marked as answer by
LarryWeiss
3 hours 52 minutes ago
August 24th, 2015 4:01pm
And the code below works fine too (at least with with POSH 3 and Windows 7). It allows you to invoke the browser currently associated with URL links. I wish there was something more rigorous I could code for that sleep 2 delay
that allows the browser to establish its window before the powershell.exe process reclaims the focus.
$url = 'www.google.com'
Invoke-Expression "start $url"
sleep 2
Add-Type -Assembly 'Microsoft.VisualBasic'
[Microsoft.VisualBasic.Interaction]::AppActivate($pid)
-
Marked as answer by
LarryWeiss
Tuesday, August 25, 2015 3:17 AM
August 24th, 2015 7:59pm
Glad it worked for you Larry! I tested it on Windows 7 and 8.1 and that seem to work fine and in all instances. Don't know if it is, like Mike pointed out, always reliable, but for now it seems to work fine. I didn't test on Windows 10, but my guess that
will work as well.
August 25th, 2015 5:24am
It is not working for me under Windows 10. The $pid passed to AppActivate is not recognized as an ID of a current process.
I'll test again later after the next significant Windows 10 update in October.
For now, Windows 7 is my main PC's OS and it is working there.
August 25th, 2015 10:32am