2

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!

6
  • Strange, 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
    – nixda
    Dec 3, 2014 at 21:38
  • @nixda Win8 x64. PSVersion returns 3.0. Thanks!
    – GiantDuck
    Dec 3, 2014 at 21:46
  • 1
    Can reproduce this using 8.1 x64 PowerShell 4.0 Dec 3, 2014 at 21:50
  • This is probably more on-topic at Stack Overflow as SO is more focused on code-related questions. I've provided an answer below, because the question is otherwise valid. and flagged for migration to Stack Overflow Dec 4, 2014 at 1:01

1 Answer 1

2

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
0

Not the answer you're looking for? Browse other questions tagged .