Albanese98
Albanese98

Reputation: 13

Powershell launch window on second screen

I need to launch two Google pages in full screen on a PC with 2 screens, one page for each screen.

Actually this is my code:

$pathToChrome = 'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe'
$tempFolder = '--user-data-dir=c:\temp' # pick a temp folder for user data
$startmode = '--start-fullscreen' # '--kiosk' is another option
$startPage1 = 'https://google.com'
$startPage2 = 'https://google.com'

Start-Process -FilePath $pathToChrome ('--new-window',  '--start-fullscreen', $startPage1) -ErrorVariable Test
Start-Process -FilePath $pathToChrome ('--new-window',  '--start-fullscreen', $startPage2) -ErrorVariable Test 

It works but it open one page over the other one. How can I do for open the second page on my second screen?

Upvotes: 0

Views: 6199

Answers (3)

Jrigo
Jrigo

Reputation: 1

Solution

Hi to do that you should do the following:

  1. Open both windows
  2. Get the Windows Handler
  3. Get Screen Dimensions
  4. Move the window to the desired position according to screen dimensions. This means that the first screen always would be (x = 0,y = 0). The second screen position would depend if is above or beside.

Functions

You can read more about the following Powershell functions if you like:

BringWindowToTop
MoveWindow

Code

This following code can help you do that (also you don't need to use the chrome path). In this example the second screen is above, so its position would be (x=0,y=-MaxScreenYSize)

# Declare the functions to be used
Add-Type @"
using System;
using System.Runtime.InteropServices;

public class User32 {
    [DllImport("user32.dll")]
    public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

    [DllImport("user32.dll", SetLastError = true)]
    public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int 
nWidth, int nHeight, bool bRepaint);

    [DllImport("user32.dll")]
    public static extern bool BringWindowToTop(IntPtr hWnd);
}
"@

$startPage1 = 'https://google.com'
$startPage2 = 'https://google.com'

# Start Process
$windows1 = Start-Process chrome.exe "--new-window --start-fullscreen 
$($startPage1 )" -PassThru
$windows2 = Start-Process chrome.exe "--new-window --start-fullscreen 
$($startPage2 )" -PassThru

# Ensure Windows Handlers Open correctly
$windowsHandle1 = $windows1.MainWindowHandle
$windowsHandle2 = $windows2.MainWindowHandle
while (-not ($windowsHandle1 -and $windowsHandle2 )) {  
    $windows1.Refresh()
    $windows2.Refresh()

    $windowsHandle1 = $windows1.MainWindowHandle
    $windowsHandle2 = $windows2.MainWindowHandle
    Start-Sleep -Milliseconds 500
}
#Get Screen Dimensions
$screenWidth = [System.Windows.Forms.Screen]::PrimaryScreen.Bounds.Width
$screenHeight = [System.Windows.Forms.Screen]::PrimaryScreen.Bounds.Height

# Declare a Hash Map of both windows
$windowData = @{
    "Window1" = @{Handle = $windowsHandle1; MaxY = -$screenHeight}
     "Window2" = @{Handle = $windowsHandle2; MaxY = 0}
}

# Focus and open each window
foreach ($key in $windowData.Keys) {
    $data = $windowData[$key]
    $handle = $data.Handle
    $maxY = $data.MaxY

    [User32]::BringWindowToTop($handle) # Bring to Front
    [User32]::MoveWindow($handle, 0, $maxY, $screenWidth, $screenHeight, 
$true) # Move Window
}

Upvotes: 0

marunio
marunio

Reputation: 1

im not sure if this is still needed for someone, but i marge two scripts to one and now its working fine :)

    $pathToChrome = 'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe'
$tempFolder = '--user-data-dir=c:\temp' # pick a temp folder for user data
$startmode = '--start-fullscreen' # '--kiosk' is another option
$startPage1 = 'www.domain1.com'enter code here
$startPage2 = 'www.domain2.com'

Start-Process -FilePath $pathToChrome ('--new-window',  '--start-fullscreen', '--user-data-dir=c:/screen1','--window-position=1680,0', $startPage1) -ErrorVariable Test 
Start-Process -FilePath $pathToChrome ('--new-window',  '--start-fullscreen', '--user-data-dir=c:/screen2','--window-position=0,0', $startPage2) -ErrorVariable Test 

Upvotes: 0

Cpt.Whale
Cpt.Whale

Reputation: 5321

This is application-specific in Windows, unfortunately. Windows processes (like explorer.exe) all use startup info stored in the registry, but that is not a standard and many applications ignore it entirely.

The only real global way around it is to emulate moving the window manually using some user32.dll methods like CherryDT's comment suggests.


To answer your example, Chromium browsers can use start flags to specify position, but it requires running each window with separate profiles:

How to open two instances of Chrome kiosk mode in different displays (Windows)

Launch Google Chrome from the command line with specific window coordinates

# open chrome on two monitors (all chrome windows must be closed first)
"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --app="http://www.domain1.com" --window-position=0,0 --kiosk --user-data-dir=c:/monitor1

"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --app="http://www.domain2.com" --window-position=1680,0 --kiosk --user-data-dir=c:/monitor2

I have found many other applications use a simple .ini file in appdata to store window size/location settings.

Upvotes: 0

Related Questions