{"id":1226,"date":"2022-10-10T08:00:09","date_gmt":"2022-10-10T08:00:09","guid":{"rendered":"https:\/\/www.nooblet.org\/blog\/?p=1226"},"modified":"2022-11-05T00:16:37","modified_gmt":"2022-11-05T00:16:37","slug":"minimize-applications-with-powershell","status":"publish","type":"post","link":"https:\/\/www.nooblet.org\/blog\/2022\/minimize-applications-with-powershell\/","title":{"rendered":"Minimize applications with PowerShell"},"content":{"rendered":"<p>I have adjusted the <a href=\"https:\/\/www.nooblet.org\/blog\/2020\/automatically-minimize-3cx-phone-for-windows\/\">previous 3CX script from 2020<\/a> to simply minimize a list of applications. The script will search for matching processes for up to 30 seconds and minimize them. I have this script run on logon. It currently minimizes both 3CX and Voicemeeter Macro Button app.<\/p>\n<pre class=\"lang:ps decode:true \" title=\"minimize-apps.ps1\" >$processes = @(\r\n    @{\r\n        Name = '3CXDesktopApp'\r\n        Result = $false\r\n    },\r\n    @{\r\n        Name = 'VoicemeeterMacroButtons'\r\n        Result = $false\r\n    }\r\n)\r\n\r\n# how long to wait (in seconds) for processes to load before we give up\r\n$wait = 30\r\n\r\n#################################################\r\n# idea from https:\/\/community.idera.com\/database-tools\/powershell\/ask_the_experts\/f\/powershell_for_windows-12\/11584\/how-to-script-clicking-on-x-to-close-window\r\nfunction Close-Window {\r\n    param(\r\n        [Parameter()]\r\n        $handle = (Get-Process -Id $pid).MainWindowHandle\r\n    )\r\n\r\n    # expose \"SendMessage\" function\r\n    $winAPI = Add-Type -MemberDefinition @'\r\n[DllImport(\"user32.dll\")]\r\npublic static extern int SendMessage(int hWnd, uint Msg, int wParam, int lParam);\r\n'@ -Name \"Win32CloseWindow\" -Namespace Win32Functions -PassThru\r\n   \r\n    # close window\r\n    $winAPI::SendMessage($handle, 0x0112, 0xF060, 0)\r\n}\r\n#################################################\r\n\r\n# Set start time, used to determine when to stop\r\n$startTime = Get-Date\r\n\r\n# Check if loop has been running longer than $wait\r\nwhile (((New-TimeSpan -Start $startTime -End (Get-Date)).TotalSeconds) -le $wait) {\r\n    # if this is still true at end of loop, we break\r\n    $found = $true\r\n    Foreach ($item in $processes.Keys) {\r\n        if ($true -ne $item.Result) {\r\n            $process = (Get-Process -Name $item.Name)\r\n            if ($process.length -gt 0) {\r\n                # minimize process\r\n                # Handles that equal 0 are already minimized\/hidden\r\n                $process.MainWindowHandle | Where-Object { [int]$_ -gt 0 } | ForEach-Object { Close-Window $_ }\r\n                # mark we have completed this one\r\n                $processes[$item].Result = $true\r\n            } else {\r\n                $found = $false\r\n            }\r\n            Start-Sleep 1\r\n        }\r\n    }\r\n    # still true after loop, so we found all the processes\r\n    if ($found) { break }\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I have adjusted the previous 3CX script from 2020 to simply minimize a list of applications. The script will search for matching processes for up to 30 seconds and minimize them. I have this script run on logon. It currently minimizes both 3CX and Voicemeeter Macro Button app. $processes = @( @{ Name = &#8216;3CXDesktopApp&#8217; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":641,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-1226","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-other"],"_links":{"self":[{"href":"https:\/\/www.nooblet.org\/blog\/wp-json\/wp\/v2\/posts\/1226","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.nooblet.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.nooblet.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.nooblet.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.nooblet.org\/blog\/wp-json\/wp\/v2\/comments?post=1226"}],"version-history":[{"count":2,"href":"https:\/\/www.nooblet.org\/blog\/wp-json\/wp\/v2\/posts\/1226\/revisions"}],"predecessor-version":[{"id":1228,"href":"https:\/\/www.nooblet.org\/blog\/wp-json\/wp\/v2\/posts\/1226\/revisions\/1228"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.nooblet.org\/blog\/wp-json\/wp\/v2\/media\/641"}],"wp:attachment":[{"href":"https:\/\/www.nooblet.org\/blog\/wp-json\/wp\/v2\/media?parent=1226"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.nooblet.org\/blog\/wp-json\/wp\/v2\/categories?post=1226"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.nooblet.org\/blog\/wp-json\/wp\/v2\/tags?post=1226"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}