title: Using Windows Powershell to Open Browsers
body: <p>I'm just learning Powershell. I found a couple of scripts online, modified them to get the files to open in Chrome and IE. The script works except that IE opens with the blank page but doesn't open all the other links in additional tabs as it does in Chrome. Additionally I get the error message below. The behavior I would like to see is every link open in Chrome and IE and then close each browser after a certain amount of time.</p>
  
  <p>Error received:</p>
  
  <pre><code>You cannot call a method on a null-valued expression.
  At line:92 char:13
  +             $ie.navigate($site );
  +             ~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
      + FullyQualifiedErrorId : InvokeMethodOnNull
  
  Remove-Variable : Cannot find a variable with the name 'ie'.
  At line:95 char:13
  +             Remove-Variable ie;
  +             ~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : ObjectNotFound: (ie:String) [Remove-Variable], ItemNotFoundException
      + FullyQualifiedErrorId : VariableNotFound,Microsoft.PowerShell.Commands.RemoveVariableCommand
  </code></pre>
  
  <p>The code is below and the links in the text file are listed below that.</p>
  
  <pre><code># How long to wait between open site commands
  $waitBetweenSitesS = 7;
  
  # How long to wait after a browser's last site before closing its window
  $waitBeforeBrowserClose = 7;
  
  # How long to wait between browsers
  $waitBetweenBrowsers = 7;
  
  # Name of the file containing the sites to open
  $siteUrlFile = "C:\Scripts\URLs.txt";
  
  
  # Browsers to start
  $browsers = @("chrome", "iexplore")
  
  # Start of the script
  
  Function Open-IETabs {
  param (
  [string[]]$sites )
  begin {
  $Ie = New-Object -ComObject InternetExplorer.Application }
  process {
  
  foreach ($Link in $sites)
  {
  $Ie.Navigate2($Link, 0x1000)
  }
  }
  end
  {
  $Ie.Visible = $true }
  }
  #$path = read-host 'Enter the path of ppm list file'
  $sites = gc "C:\Scripts\URLs.txt"
  Open-IETabs -Url $sites
  
  # Browsers
     foreach ($browser in $browsers)
     {
  
  # Sites
        $siteCount = 0;
        foreach ($site in $sites)
        {
           $siteCount++;
  
           if ($siteCount -eq 1)
           {
              if ($browser -eq "chrome" -or $browser -eq "iexplore")
              {
                 # Start the browser with an empty tab because the first page load is currently not captured by uberAgent
                 $process = Start-Process -PassThru $browser "about:blank"
              }
              else
              {
                 # Start the browser with the first site
                 $process = Start-Process -PassThru $browser $site
              }
  
              # Store the browser's main process (the first one started)
              $browserProcess = $process;
  
              # Wait for the window to open
              while ($process.MainWindowHandle -eq 0)
              {
                 Start-Sleep 1
              }
  
              if ($browser -eq "chrome" -or $browser -eq "firefox")
              {
                 # Open the first site in a new tab
                 Start-Process $browser $site
              }
           }
           elseif ($browser -eq "iexplore")
           {
              # Additional IE tabs need to be opened differently, or new windows will be created instead
  
              $navOpenInNewTab = 0x800;
  
              # Get running Internet Explorer instances
              $app = New-Object -ComObject shell.application;
  
              # Grab the last opened tab
              $ie = $app.Windows() | Select-Object -Last 1;
  
              # Open the site in a new tab
              $ie.navigate($site, $navOpenInNewTab);
  
              # Release the COM objects
              Remove-Variable ie;
              Remove-Variable app;
           }
           else
           {
              # Addition tabs in Chrome/Firefox
              Start-Process $site #$browser 
           }
  
           Start-Sleep $waitBetweenSitesS;
        }
  
        Start-Sleep $waitBeforeBrowserClose;
  
        # Close the browser
        $browserProcess.CloseMainWindow();
        $browserProcess = $null;
  
        Start-Sleep $waitBetweenBrowsers;
  
        }
  
  https://mail.google.com/mail/u/0/#inbox
  https://docs.google.com/document/d/1hOc4bdEQ1-KJ5wOsiCt4kVQB-xaHuciQY6Y4X_I7dYA/edit
  https://www.google.com/maps/
  https://twitter.com/
  https://onedrive.live.com/edit.aspx?cid=740de493111072ca&amp;page=view&amp;resid=740DE493111072CA!108&amp;parId=740DE493111072CA!106&amp;app=PowerPoint
  https://outlook.live.com/mail/inbox
  https://www.dropbox.com/h
  https://www.nytimes.com/
  https://www.nbcnews.com/
  https://foxnews.com/
  </code></pre>