none
How do you maximize an Internet Explorer window via a Powershell script? RRS feed

  • Question

  • How do you maximize an Internet Explorer window via a Powershell script?

    I grabbed a copy of a "PowerShell Script to Open Your Gmail Account" by ankitwww over at the Powershell TechNet Script Center Repository, and modified for my own Gmail accounts and it works great.

    I then decided make a variation of the script to automatically sign me on to my Microsoft Connect account. After a little hemming and hawing, I got that to work too.

    But with one exception - the IE window opens as "Normal" instead of the way I prefer it as "Maximized".  So my question again is how do you maximize an Internet Explorer window via a Powershell script?

    I've googled around and found VB and Jscript stuff to do it (neither of which do I know *g*), but I'd like to do it natively in Powershell.

    Any suggestions would be appreciated by this Powershell newbie.

    TIA!
    Sunday, December 20, 2009 1:38 AM

Answers

All replies

  • Hey

    I would assume you could just use the "fullscreen" property of the "internetexplorer.application" object

    http://msdn.microsoft.com/en-us/library/aa752054%28VS.85%29.aspx

    So in the powershell script i'd assume you could just add another line of code after:
    $ie.visible = $true;
    $ie.fullscreen = $true;
    Give that a try. Hope that helps

    Cheers Matt :)
    Sunday, December 20, 2009 3:13 AM
    Moderator
  • Hello Gigory,

    To achieve your goal of maximizing an IE window, we can use PowerShell with P/Invoke.NET. Thankfully, the InternetExplorer.Application COM type exposes a HWND property, which contains the handle to the window. All we need to do is find the appropriate Win32 function to maximize a window, based on the HWND value, and we should be good to go.

    Try this code out:

    $ie = New-Object -ComObject InternetExplorer.Application
    $ie.Visible = $true
    $ie.Navigate("www.google.com")

    $sw = @'
    [DllImport("user32.dll")]
    public static extern int ShowWindow(int hwnd, int nCmdShow);
    '@

    $type = Add-Type -Name ShowWindow2 -MemberDefinition $sw -Language CSharpVersion3 -Namespace Utils -PassThru
    $type::ShowWindow($ie.hwnd, 3) # 3 = maximize


    Sources

    http://www.pinvoke.net/default.aspx/user32.ShowWindow
    http://www.leeholmes.com/blog/PowerShellPInvokeWalkthrough.aspx
    http://www.codegod.de/webappcodegod/win32-window-handler-in-c-AID134.aspx

    Hope this helps!

    Cheers,
    Trevor Sullivan
    Consultant | 1E Inc.
    http://trevorsullivan.wordpress.com
    • Marked as answer by Gigory Monday, December 21, 2009 12:37 AM
    Sunday, December 20, 2009 3:56 AM
    Moderator
  • Thanks for responding Matthew!

    A good suggestion though no cigar. :-)

    Your suggestion makes for a "fullscreen" IE instance rather than a "maximized" IE instance.

    I do appreciate the thought, and more importantly, that MSDN link since it gives me an excellent source for putzing around with IE.

    Thanks again!
    Monday, December 21, 2009 12:35 AM
  • Mucho thanks Trevor!

    That works just fine and solves the "maximize" IE question!

    And many thanks for the source links to further reduce my ignorance!
    Monday, December 21, 2009 12:37 AM
  • You're welcome :) I wasn't trying to make you feel ignorant, by the way. I was merely providing you the sources to the content, which I used, to come up with my solution for you. Taking sole credit for the solution not be appropriate; I merely put together different pieces of what other people had created.

    :)

    Cheers,
    Trevor Sullivan
    Consultant | 1E Inc.
    http://trevorsullivan.wordpress.com
    Monday, December 21, 2009 5:37 AM
    Moderator
  • Rest assured Trevor that you had no part to play in my feeling ignorant. The pleasure of feeling ignorant is all mine. LOL!

    Ta again for the solution as well as the links!

    Monday, December 21, 2009 11:57 PM
  • Worked perfectly for me. Thank you very much.
    Tuesday, May 30, 2017 2:26 PM