# Set the screen index of the second monitor (0 for the primary, 1 for the second, and so on) $screenIndex = 1 # Get the screen resolution of the second monitor $screen = [System.Windows.Forms.Screen]::AllScreens[$screenIndex] $screenWidth = $screen.Bounds.Width $screenHeight = $screen.Bounds.Height # Initialize an array to store window information $windowInfo = @() # Get all non-minimized windows $nonMinimizedWindows = Get-Process | Where-Object { !$_.MainWindowTitle -eq '' -and !$_.MainWindowTitle.StartsWith("Default IME") } # Loop through each window and collect information foreach ($window in $nonMinimizedWindows) { # Get the window handle (HWND) for inter-process communication $hwnd = $window.MainWindowHandle # Get the window rectangle (position and dimensions) $rect = New-Object -TypeName System.Drawing.Rectangle [System.Windows.Forms.NativeMethods]::GetWindowRect($hwnd, [ref]$rect) # Check if the window is on the second screen if ($rect.X -ge $screen.Bounds.Left -and $rect.X -lt $screen.Bounds.Right) { $windowName = $window.MainWindowTitle $x = $rect.X $y = $rect.Y $width = $rect.Width $height = $rect.Height # Add window information to the array $windowInfo += @{ WindowName = $windowName X = $x Y = $y Width = $width Height = $height ScreenWidth = $screenWidth ScreenHeight = $screenHeight } } } # Set the collected window information as global variables $global:WindowInfo = $windowInfo $global:SecondScreenWidth = $screenWidth $global:SecondScreenHeight = $screenHeight # Print all variables in the PowerShell window $global:WindowInfo $global:SecondScreenWidth $global:SecondScreenHeight