1
+ # This function should minimize the window using the Win32 API
2
+ # It receives a string with the first part of the window title
3
+ # and minimizes the window if it finds a match among the open windows
4
+ function Minimize-Window {
5
+ param (
6
+ [string ]$windowTitlePart
7
+ )
8
+
9
+ Write-Host " Starting window minimization process..." - ForegroundColor Cyan
10
+ Write-Host " Looking for windows with title containing: '$windowTitlePart '" - ForegroundColor Cyan
11
+
12
+ # Get the handle of the current process
13
+ $currentProcess = Get-Process - Id $PID
14
+ $currentProcessHandle = $currentProcess.MainWindowHandle
15
+
16
+ # Get all open windows
17
+ $windows = Get-Process | Where-Object { $_.MainWindowHandle -ne 0 }
18
+
19
+ Write-Host " Found $ ( $windows.Count ) windows with valid handles" - ForegroundColor Gray
20
+
21
+ $matchFound = $false
22
+
23
+ foreach ($window in $windows ) {
24
+ Write-Verbose " Checking window: $ ( $window.MainWindowTitle ) (Handle: $ ( $window.MainWindowHandle ) )"
25
+
26
+ if ($window.MainWindowTitle -like " *$windowTitlePart *" ) {
27
+ Write-Host " Match found! Window title: $ ( $window.MainWindowTitle ) " - ForegroundColor Green
28
+
29
+ # Minimize the window
30
+ try {
31
+ [void ][System.Runtime.InteropServices.Marshal ]::GetDelegateForFunctionPointer(
32
+ [System.Runtime.InteropServices.Marshal ]::GetProcAddress(
33
+ [System.Runtime.InteropServices.Marshal ]::GetHINSTANCE([System.Reflection.Assembly ]::GetExecutingAssembly().GetModules()[0 ]),
34
+ " ShowWindow"
35
+ ),
36
+ [System.IntPtr ]
37
+ )($window.MainWindowHandle , 2 ) # 2 = SW_MINIMIZE
38
+ Write-Host " Successfully minimized window: $ ( $window.MainWindowTitle ) " - ForegroundColor Green
39
+ $matchFound = $true
40
+ }
41
+ catch {
42
+ Write-Host " Error minimizing window: $ ( $_.Exception.Message ) " - ForegroundColor Red
43
+ }
44
+ }
45
+ }
46
+
47
+ # If no match was found, return a list of all window handles
48
+ if (-not $matchFound ) {
49
+ Write-Host " No windows found matching the pattern: '$windowTitlePart '" - ForegroundColor Yellow
50
+ Write-Host " Here's a list of all available windows:" - ForegroundColor Yellow
51
+
52
+ $windowList = $windows | ForEach-Object {
53
+ [PSCustomObject ]@ {
54
+ Title = $_.MainWindowTitle
55
+ Handle = $_.MainWindowHandle
56
+ ProcessName = $_.ProcessName
57
+ Id = $_.Id
58
+ }
59
+ }
60
+
61
+ $windowList | Format-Table - AutoSize
62
+ return $windowList
63
+ }
64
+ }
0 commit comments