Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Maximized IE windows launch from PowerShell

Status
Not open for further replies.

Balazs Torocsik

IS-IT--Management
Jun 12, 2019
1
AT
Since I did not find the correct solution anywhere, including the below VBScript thread (which is closed), I would like to to share my solution to this problem in PowerShell.

thread329-1364653

Code:
#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="[URL unfurl="true"]http://www.google.com/"[/URL]
#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
$hwnd

#Magic  -- in my case, $hwnd is an array with 2 IDs, but only 1 integer can be used as IntPtr for ShowWindowAsync, so I am taking the later one.
if($hwnd[0].ToInt32() -gt $hwnd[1].ToInt32()){
    $type::ShowWindowAsync($hwnd[0], $Maximized)
}else{
    $type::ShowWindowAsync($hwnd[1], $Maximized)
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top