r/PowerShell • u/dcutts77 • Nov 26 '25
Script to Bring Off Screen Windows to Primary Monitor
# Bring off screen windows back onto the primary monitor
Add-Type -AssemblyName System.Windows.Forms
Add-Type @"
using System;
using System.Runtime.InteropServices;
using System.Text;
public class Win32 {
public delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
[DllImport("user32.dll")]
public static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool IsWindowVisible(IntPtr hWnd);
[DllImport("user32.dll", SetLastError = true)]
public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool MoveWindow(
IntPtr hWnd,
int X,
int Y,
int nWidth,
int nHeight,
bool bRepaint
);
[StructLayout(LayoutKind.Sequential)]
public struct RECT {
public int Left;
public int Top;
public int Right;
public int Bottom;
}
}
"@
# Get primary screen bounds
$screen = [System.Windows.Forms.Screen]::PrimaryScreen.Bounds
$windows = New-Object System.Collections.Generic.List[object]
# Enumerate top level windows
$null = [Win32]::EnumWindows(
{ param($hWnd, $lParam)
if (-not [Win32]::IsWindowVisible($hWnd)) {
return $true
}
# Get window title
$sb = New-Object System.Text.StringBuilder 256
[void][Win32]::GetWindowText($hWnd, $sb, $sb.Capacity)
$title = $sb.ToString()
# Skip untitled windows like some tool windows
if ([string]::IsNullOrWhiteSpace($title)) {
return $true
}
# Get window rectangle
[Win32+RECT]$rect = New-Object Win32+RECT
if (-not [Win32]::GetWindowRect($hWnd, [ref]$rect)) {
return $true
}
$width = $rect.Right - $rect.Left
$height = $rect.Bottom - $rect.Top
$windows.Add(
[PSCustomObject]@{
Handle = $hWnd
Title = $title
Left = $rect.Left
Top = $rect.Top
Right = $rect.Right
Bottom = $rect.Bottom
Width = $width
Height = $height
}
) | Out-Null
return $true
},
[IntPtr]::Zero
)
# Function to decide if window is completely off the primary screen
function Test-OffScreen {
param(
[int]$Left,
[int]$Top,
[int]$Right,
[int]$Bottom,
$screen
)
# Completely to the left or right or above or below
if ($Right -lt $screen.Left) { return $true }
if ($Left -gt $screen.Right) { return $true }
if ($Bottom -lt $screen.Top) { return $true }
if ($Top -gt $screen.Bottom){ return $true }
return $false
}
Write-Host "Scanning for off-screen windows..." -ForegroundColor Cyan
$offScreenCount = 0
foreach ($w in $windows) {
if (Test-OffScreen -Left $w.Left -Top $w.Top -Right $w.Right -Bottom $w.Bottom -screen $screen) {
$offScreenCount++
# Clamp size so it fits on screen
$newWidth = [Math]::Min($w.Width, $screen.Width)
$newHeight = [Math]::Min($w.Height, $screen.Height)
# Center on primary screen
$newX = $screen.Left + [Math]::Max(0, [int](($screen.Width - $newWidth) / 2))
$newY = $screen.Top + [Math]::Max(0, [int](($screen.Height - $newHeight) / 2))
Write-Host "Moving window: '$($w.Title)' to ($newX, $newY)" -ForegroundColor Yellow
$result = [Win32]::MoveWindow(
$w.Handle,
[int]$newX,
[int]$newY,
[int]$newWidth,
[int]$newHeight,
$true
)
if (-not $result) {
Write-Warning "Failed to move window: '$($w.Title)'"
}
}
}
if ($offScreenCount -eq 0) {
Write-Host "No off-screen windows found." -ForegroundColor Green
} else {
Write-Host "`nRepositioned $offScreenCount window(s) to the primary monitor." -ForegroundColor Green
}
Write-Host "`nPress any key to exit..."
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
50
Upvotes
17
u/dcutts77 Nov 26 '25
I wrote this script for when users are logged in and a window they opened when they have multiple monitors is now missing and they need to see it, it will pop it back to the primary monitor.
17
u/lan-shark Nov 26 '25
I see what it does, but what situations do you need this in that aren't handled by
Win + Shift + <arrow key>?5
u/GMginger Nov 26 '25
Great work! Will try out later, I swap around between different docks at work so a few times a week I end up with a window off screen - this'd be much easier than Alt-Space, M and trying and tease it back (which as well as being a pain doesn't always work).
4
u/purplemonkeymad Nov 26 '25
Rest in Peace collate windows. This is what we had to do when they removed you.
3
u/VeryRareHuman Nov 26 '25
I had this issue on Windows 11. Couldn't bring the window from Off screen with hot keys to move window, maximize it or move it to next virtual screen.
Thanks for writing this script, I will check and run it.
3
u/jrobiii Nov 27 '25
In complete honesty, I have not read the script (TL;DR).
The way I fix this (doesn't work for modal dialogs):
If the window off screen is full screen, you'll need to do the first step above and press {R} to restore the window
After that all the above steps will work. This can also be automated and assigned to a hotkey using AutoHotkey or the like.
3
u/CtrlAltERP Nov 27 '25
What if PS is off screen? :O
Jk ofc. Looks great, will try it out. Thanks!
2
u/VoltageOnTheLow Nov 27 '25
Gotta love how easily (relatively speaking) PowerShell lets you solve odd problems like this. Nice work!
1
u/CryktonVyr Nov 28 '25