Focus IE window in powershell
I've created a Open-InternetExplorer
function that creates an IE COM object, navigates to a URL, sets IE as the foreground window, and optionally full screen.
It should be noted that when the -InForeground
switch is used a call is made to the native SetForegroundWindow Win32 API. There are some situations in which this function will not change the foreground window. These situations are outlined in the MSDN documentation for the function.
function Add-NativeHelperType
{
$nativeHelperTypeDefinition =
@"
using System;
using System.Runtime.InteropServices;
public static class NativeHelper
{
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool SetForegroundWindow(IntPtr hWnd);
public static bool SetForeground(IntPtr windowHandle)
{
return NativeHelper.SetForegroundWindow(windowHandle);
}
}
"@
if(-not ([System.Management.Automation.PSTypeName] "NativeHelper").Type)
{
Add-Type -TypeDefinition $nativeHelperTypeDefinition
}
}
function Open-InternetExplorer
{
Param
(
[Parameter(Mandatory=$true)]
[string] $Url,
[switch] $InForeground,
[switch] $FullScreen
)
if($InForeground)
{
Add-NativeHelperType
}
$internetExplorer = new-object -com "InternetExplorer.Application"
$internetExplorer.navigate($Url)
$internetExplorer.Visible = $true
$internetExplorer.FullScreen = $FullScreen
if($InForeground)
{
[NativeHelper]::SetForeground($internetExplorer.HWND)
}
return $internetExplorer
}
While the script provided should work, there are some potential issues in relation to resource management.
I'm not sure if I need to do anything specific in relation to returning the COM object. It's possible that either .NET or PowerShell handles this itself, but if not, there's a possibility of a resource leak.
I'm also unsure what I should do (if anything) in relation to making sure that InternetExplorer.HWND
is valid before it's passed into SetForegroundWindow
Example Usage:
. .\Open-InternetExplorer.ps1
Open-InternetExplorer -Url www.example.com -FullScreen -InForeground
Related videos on Youtube
GiantDuck
Updated on September 18, 2022Comments
-
GiantDuck over 1 year
My code:
$ie = new-object -com "InternetExplorer.Application" $ie.navigate("http://localhost") $ie.visible = $true $ie.fullscreen = $true
However, after fullscreen, the window still appears behind the Windows taskbar. When I click the window to give it focus, the taskbar falls behind and it appears how I want it to. How can I do this programmatically? Thank you!
-
nixda over 9 yearsStrange, I cannot reproduce this. What windows (8 or 8.1?) and Powershell version do you use. Mine is Windows 7 x64 and Powershell 7.0 and the taskbar is hidden immediatly
-
GiantDuck over 9 years@nixda Win8 x64.
PSVersion
returns 3.0. Thanks! -
Crippledsmurf over 9 yearsCan reproduce this using 8.1 x64 PowerShell 4.0
-
nhinkle over 9 years@GiantDuck in the future please do not cross-post questions to multiple Stack Exchange sites. Select the site which best fits your question before asking it. If you ask on the wrong site, either wait for it to be migrated, or delete it yourself and re-ask on the appropriate site. Thanks.
-