Open IE maximized from Powershell script?

Tag: powershell Author: clarity123321 Date: 2009-08-10

Pardon me if this is too simple a question, but I'm not finding anything in the help files or online so far regarding doing this. I'm opening up a new browser window to test the login/logout feature of a web based application, but I want to open the IE window in maximized mode. I could set the size as:

$ie.height = 1024 $ie.width - 768

But is there a keyword or anything that I can use to just open it up maximized automatically or would I need to query the screen size first and then fill in the values from that query?

/matt

Best Answer

(new-object -com wscript.shell).run("url",3)

comments:

That opens IE but doesn't maximize it - at least on my Win7 PC.
It launches the default browser and in my case it was maximized on my Win7 machine - but that was in Firefox. I'm going to change it to launch IE and see what I get. Thank you both for your input, I appreciate it. /matt
Setting my default browser to IE, it launches in maximized mode. Perfect!
So it does not work if you already have an instance of IE open and not maximized. It just adds another tab to the existing IE. If you don't have IE open then just use the built-in cmdlet: Start-Process "url" -WindowStyle Maximized.

Other Answer1

If you have the PowerShell Community Extensions 1.2 (PSCX) installed on PowerShell 2.0, I have verified that this works:

Pscx\Start-Process IExplore.exe; Start-Sleep 3; $hwnd = Get-ForegroundWindow
$sig = @'
[DllImport("user32.dll")] 
public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
'@
Add-Type -MemberDefinition $sig -name NativeMethods -namespace Win32
[Win32.NativeMethods]::ShowWindowAsync($hwnd, 3)

It is a little dicey because it is using a wait (start-sleep) of 3 secs to wait for IE to open and then it uses a PSCX cmdlet to get the window handle of the foreground window. If you only have one instance of IExplore running then you can use this to get that handle:

@(Get-Process IExplore)[0].MainWindowHandle

PowerShell 2.0 is required for the Add-Type support that allows us to call down to the Win32 API.

BTW from a quick Bing search it seems that getting IE to start maximized is a pretty common problem. For instance, with Start-Process you can specify -WindowStyle Maximized but IE doesn't honor that.

comments:

Thanks for the suggestion, I'll pop that in and see what I get. A bit embarrassed you found info so easily on Bing, I googled multiple variations on opening the window in maximized size and most returns were dealing with opening the Powershell window! All in how you ask I guess!
Well to be clear I just searched on "Open IExplore Maximized" in Bing and noticed that even outside the context of PowerShell, people have problems with this.

Other Answer2

Solved the Problem of starting IE maximized with following:

Function maxIE
{
param($ie)
$asm = [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

    $screen = [System.Windows.Forms.Screen]::PrimaryScreen.Bounds
    $ie.Width = $screen.width
    $ie.Height =$screen.height
    $ie.Top =  0
    $ie.Left = 0
}


cls
$ie = new-object -com "InternetExplorer.Application"
$ie.visible = $true
maxIE $ie
while ($ie.busy) {sleep -milliseconds 50}
$ie.navigate("http://www.google.com")