Problem: We have a large television we use for presentations, that isn't exactly 1920×1080 px. It's off and I can't control it. What I can control is my windows laptop that I connect to it. I have been looking for ways to get whatever I need displayed, to move over to the projector, set a position that is offset for looking good on the tv, and set the width/height for the same.
What's been done: I can use Boe Prox's Get-Window script (https://blogs.technet.microsoft.com/heyscriptingguy/2015/12/26/weekend-scripter-manage-window-placement-by-using-pinvoke/), and that worked fine, to get me the dimensions and offset I need. However, his Set-Window errors on me in both WinPosh 5 and Posh 6 with and without Admin privilege. Other potential solutions produced similar errors, so I decided to stay with Prox's script, as he's an expert by my standards.
I am evaluating solutions mentioned here, https://stackoverflow.com/questions/10392620/how-can-a-batch-file-run-a-program-and-set-the-position-and-size-of-the-window/ as possible workarounds. However, a PowerShell solution without dependencies on anything third party would be ideal.
The question is:
Has anyone solved how to get either Mr. Prox's Set-Window or anything else to set a windows position and size in straight v5 or v6 PowerShell?
Error Message in Posh 5:
Cannot convert argument "hWnd", with value: "System.Object[]", for "GetWindowRect" to type "System.IntPtr": "Cannot
convert the "System.Object[]" value of type "System.Object[]" to type "System.IntPtr"."
At Z:\scripts\Set-Window.ps1:90 char:9
+ $Return = [Window]::GetWindowRect($Handle,[ref]$Rectangle)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument
Error Messages in Posh 6:
PS C:\> Set-Window -ProcessName notepad -X 1911 -Y "-369" -Width 266 -Height 113
Method invocation failed because [Window] does not contain a method named 'MoveWindow'.
At Z:\scripts\Set-Window.ps1:98 char:13
+ $Return = [Window]::MoveWindow($Handle, $x, $y, $Width, $ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
PS C:\> Set-Window -ProcessName firefox -X "-9" -Y "-9" -Width "1938" -Height "1050"
Cannot convert argument "hWnd", with value: "System.Object[]", for "GetWindowRect" to type "System.IntPtr": "Cannot convert the "System.Object[]" value of type "System.Object[]" to type "System.IntPtr"."
At Z:\scripts\Set-Window.ps1:90 char:9
+ $Return = [Window]::GetWindowRect($Handle,[ref]$Rectangle)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument
Note: These errors occur in Windows 7 (no access to Win10 for a while).
Update: I had noticed Set (Many) Programs' Window Size/Position, but the UIAutomation module isn't maintained (codeplex is archived, and last blog post by the author for it, was Feb 2014).
Best Answer
By default,
Get-Process
cmdlet returns aSystem.Diagnostics.Process
object - or an array of such object if there are more matching processes. Unfortunately, the originalSet-Window.ps1
script does not reflect the latter scenario.Output using improved script:
Edit.
Improved script (changes commented): see revision history.
Enhanced script, the latest version:
Id
(process Id);Some minor changes to verbose and warning output.