2

I can open internet explorer doing this, is it posible doing the same with firefox?

$ie = new-object -comobject InternetExplorer.Application;

$ie.visible = $true;
#$ie2 = $ie.Width = 200;

$ie.top = 0; $ie.width = 1000; $ie.height = 600 ; $ie.Left = 200;

$ie.navigate('https://google.com');
daniel gon
  • 99
  • 2
  • 3
  • 11

2 Answers2

2

See this discussion and answer

Setting window size and position in PowerShell 5 and 6

# Call Firefox and set to window position on its process
Start-Process -FilePath 'C:\Program Files\Mozilla Firefox\firefox.exe'
Start-Sleep -Seconds 2
Set-Window -ProcessName firefox -x 1 -y 1 -Width 615 -Height 345 -Passthru
postanote
  • 15,138
  • 2
  • 14
  • 25
  • This was fully tested on different machines before posting. So, I know it works. – postanote Sep 08 '18 at 00:12
  • OK! I got it working. I needed to change the ExecutionPolicy of my powerShell. Thanks! – daniel gon Sep 10 '18 at 16:47
  • I also need to load the Set-Window script firts – daniel gon Sep 10 '18 at 16:58
  • No worries, and many have gotten bit by the execution policy setting with other efforts as well, and yep, all modules, functions have to be loaded (if the cannot be auto loaded) before they will work for you. So, if this is an answer you can work with. mark it as answered as well, for reference as other may look to do the same. The answer works for virtually any running process GUI app, not just Firefox. At least from the test bank I used. – postanote Sep 10 '18 at 20:25
1

Firefox has command-line arguments for width and height, but I could not find anything for window position.

This works in 61.0.2. You may have to modify the parameters to FindWindow() based on your usage.

Note that this is not the most robust code ever, but it may suit your needs.

& "C:\Program Files\Mozilla Firefox\firefox.exe" -width 1000 -height 600 https://google.com

$Assem = ( 
    "System.Runtime.InteropServices"
    ) 

$Source = @" 
using System;
using System.Runtime.InteropServices; // For the P/Invoke signatures.

namespace Code42 {

    public static class PositionWindowDemo
    {

        // P/Invoke declarations.

        [DllImport("user32.dll", SetLastError = true)]
        static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImport("user32.dll", SetLastError = true)]
        static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);

        const uint SWP_NOSIZE = 0x0001;
        const uint SWP_NOZORDER = 0x0004;

        public static void MoveWindow(string name)
        {
            // Find (the first-in-Z-order) Notepad window.
            IntPtr hWnd = FindWindow(null, name);

            // If found, position it.
            if (hWnd != IntPtr.Zero)
            {
                // Move the window to (0,0) without changing its size or position
                // in the Z order.
                SetWindowPos(hWnd, IntPtr.Zero, 0, 0, 0, 200, SWP_NOSIZE | SWP_NOZORDER);
            }
        }

    }

}
"@ 

Add-Type -ReferencedAssemblies $Assem -TypeDefinition $Source -Language CSharp  

[Code42.PositionWindowDemo]::MoveWindow("Google - Mozilla Firefox")

Sources:

Dan Wilson
  • 3,937
  • 2
  • 17
  • 27