Matt Dewey
Matt Dewey

Reputation: 137

Open IE maximized from Powershell script?

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

Upvotes: 3

Views: 17118

Answers (6)

D4lan
D4lan

Reputation: 11

Just incase anyone else needs help I ended up just calling up iexplore maximized and then connected to it. You need to sleep because it calls too fast to connect. I'm sure there is a better way but I couldn't figure it out.

start iexplore -WindowStyle maximized
Start-Sleep -seconds 1
$ie = (New-Object -COM "Shell.Application").Windows() | ? { $_.Name -eq "Internet Explorer" }
$ie.navigate("URL")

Upvotes: 1

#We will use the Win32 API function ShowWindowAsync, and spawn an IE Window Maximized.
#Parameters can be used for ShowWindowAsync
$Hide = 0
$Normal = 1
$Minimized = 2
$Maximized = 3
$ShowNoActivateRecentPosition = 4
$Show = 5
$MinimizeActivateNext = 6
$MinimizeNoActivate = 7
$ShowNoActivate = 8
$Restore = 9
$ShowDefault = 10
$ForceMinimize = 11

#Specify an interwebs address :)
$URL="http://www.google.com/"
#Create internetexplorer.application object
$IE=new-object -com internetexplorer.application
#Set some parameters for the internetexplorer.application object
$IE.TheaterMode = $False
$IE.AddressBar = $True
$IE.StatusBar = $False
$IE.MenuBar = $True
$IE.FullScreen = $False
$IE.visible = $True
#Navigate to the URL
$IE.navigate2($URL)

#the C#-style signature of an API function
$code = '[DllImport("user32.dll")] public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);'

#add signature as new type to PowerShell (for this session)
$type = Add-Type -MemberDefinition $code -Name myAPI -PassThru

#Magic:
$type::ShowWindowAsync($IE.HWND[0], $Maximized)

Upvotes: 0

Kevin
Kevin

Reputation: 2416

I couldn't get any specific answer to work, but did get a combination to work. The order in which these are called is important else it doesn't work.

Full Example:

$ie = New-Object -Com "InternetExplorer.Application"
$urls = @("http://www.google.com","http://www.yahoo.com")
$ie.Visible = $true
CLS
write-output "Loading pages now..."
#Maximize IE window
$asm = [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$screen = [System.Windows.Forms.Screen]::PrimaryScreen.Bounds
$ie.height = $screen.height
#Open webpages
foreach ($link in $urls) {
   $ie.Navigate2($link, 0x1000) 
}
#close first blank tab
$sa = New-Object -ComObject Shell.Application
$tab = $sa.windows() | Where {$_.Name -match 'Internet Explorer' -and     $_.LocationName -eq ''}
$tab.quit()

Upvotes: 2

icnivad
icnivad

Reputation: 2291

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")

Upvotes: 4

Shay Levy
Shay Levy

Reputation: 126842

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

Upvotes: 4

Keith Hill
Keith Hill

Reputation: 201832

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.

Upvotes: 1

Related Questions