Open IE maximized from Powershell script?

Forgive me if this is an overly simple question, but I haven't found anything in the help files or online regarding this yet. I open a new browser window to test the login / logout functionality of a web app, but I want to open the IE window in maximized mode. I could set the size like:

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

But is there a keyword or anything I can use to just open it automatically, or do I need to ask for the screen size first and then fill in the values ​​from that query?

/ matt

+2


source to share


5 answers


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



+4


source


Fixed the problem of launching IE with maximum accuracy:



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

      

+3


source


I couldn't get any specific answer to work, but did get the combination to work. The order in which they are named is even more important, but it doesn't work.

Complete 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()

      

+2


source


If you have PowerShell Community Extensions 1.2 (PSCX) installed in PowerShell 2.0, I 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's a little risky because it uses wait (start-sleep) for 3 seconds to wait for IE to open, and then it uses the PSCX cmdlet to get the foreground window handle. If you only have one instance of IExplore, you can use it to get this handle:

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

      

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

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

+1


source


#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

#get the window handle
$hwnd =(Get-Process -Name "iexplore").MainWindowHandle

#Convert string handle array to integer array
$hwndINT=$hwnd | % {[int]$_}

#Get maximum (=last/latest) handle of the integer array, and re-cast it to [System.IntPtr]
[System.IntPtr]$hwndPtr=[int]$($hwndINT | measure -Maximum).Maximum

#Magic:
$type::ShowWindowAsync($hwndPtr, $Maximized)

      

0


source







All Articles