Browsing in Internet Explorer via PowerShell

The purpose of this article is to share an example that I often use in my training deliveries of PowerShell.

 

The idea is to open the Internet Explorer, navigate to the bing page, enter a text and click in the search button.

 

The first step is to instantiate the COM object in Internet Explorer and store the instance of it in the variable $IE as: 

 $IE= new-object -ComObject "InternetExplorer.Application"

The next step is to run the Navigate2 method of $IE variable as:

 

 $IE.navigate2(“https://www.bing.com")

To see the available methods of the $IE variable, type the following command: 

 $IE | gm 

 

For more details see the article: PowerShell | Objects

 

The following command uses a loop (While) to wait for the page to be loaded:

 

  while ($IE.busy) {
 start-sleep -milliseconds 1000 #aguarda 1 segundo antes de continuar
 } 

 

The following command will make visible the IE so that the page is ready to be displayed: 

 

 $IE.visible=$true 

 

The following code is used to locate the sb_form_q element that corresponds to the bing search box:

 

 $IE.Document.getElementById("sb_form_q").value="PowerShell Scripting Guy blog" 

 

For more details on how to identify what is the name of the HTML elements of a page, see the article: Internet Explorer Troubleshooting – part 1

 

The next step is to run the search button's Click method.

 

 $IE.Document.getElementById("sb_form_go").Click(); 

 

I hope you enjoyed. Until the next.