Skip to content

Instantly share code, notes, and snippets.

@blakeNaccarato
Last active April 6, 2025 21:53
Show Gist options
  • Save blakeNaccarato/95b576da40b016a5e1697d96fc09bfc8 to your computer and use it in GitHub Desktop.
Save blakeNaccarato/95b576da40b016a5e1697d96fc09bfc8 to your computer and use it in GitHub Desktop.
Monitor setup for Sunshine/Moonlight streaming
<#.SYNOPSIS
Set primary monitor resolution and scaling. Optionally move/maximize windows.
#>
Param(
# Path to MultiMonitorTool.exe (https://www.nirsoft.net/utils/multi_monitor_tool.html)
[Parameter(Mandatory)][string]$Tool,
# Primary monitor width
[int]$Width,
# Primary monitor height
[int]$Height,
# Primary monitor DPI scaling
[int]$Scale,
# Move windows to the primary monitor
[switch]$Move,
# Maximize windows when moving them
[switch]$Maximize
)
#? Set resolution and scale
$Name = 'Primary' #? Sunshine streams the primary monitor
if ($Width -or $Height) {
if (!$Width) { $Width = $Height }
if (!$Height) { $Height = $Width }
& $Tool /SetMonitors "Name=$Name Width=$Width Height=$Height"
}
if ($Scale) { & $Tool /SetScale "Name=$Name $Scale" }
#? If not moving windows, we're done
if (!$Move) { return }
#? Move windows
if ($Width -or $Height -or $Scale) { Start-Sleep 3.0 }
& $Tool /MoveWindow 'Primary' 'All'
#? If not maximizing, we're done
if (!$Maximize) { return }
#? Define Win32 API function to maximize windows
Add-Type -MemberDefinition @'
[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
'@ -Name 'Win32ShowWindow' -Namespace 'User32'
#? Maximize windows using the Win32 API
$ShowWindowMaximized = 3
(Get-Process | Where-Object { $_.MainWindowTitle }).MainWindowHandle |
ForEach-Object { [User32.Win32ShowWindow]::ShowWindow($_, $ShowWindowMaximized) } |
Out-Null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment