Forked from NotoriousPyro/OutlookAutostartAndMinimise.ps1
Created
November 18, 2025 17:54
-
-
Save h8nor/e4e4e618ba625e5479a3c4f33490ed83 to your computer and use it in GitHub Desktop.
Automatically finds, runs and places Outlook into the system tray.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <# | |
| .SYNOPSIS | |
| Automatically finds, runs and places Outlook into the system tray. | |
| .DESCRIPTION | |
| Automatically finds, runs and places Outlook into the system tray. | |
| By default, Outlook will start full-screen even when you have the option to minimize tray enabled and start the app as minimized. | |
| This requires that the OUTLOOK.EXE tray icon is enabled and that the minimize to tray option is on. | |
| Author: NotoriousPyro (Craig Crawford) | |
| Author: h8nor | |
| #> | |
| $autostartOutlook = $true; $loops = 0 | |
| $chrome = "$Env:Programfiles\Google\Chrome\Application\chrome.exe" | |
| if (Test-Path -Path $chrome -PathType Leaf) { | |
| Write-Output "Starting CHROME from $chrome" | |
| Start-Process -FilePath $chrome -ArgumentList "--disable-features=LockProfileCookieDatabase --no-startup-window /prefetch:5" | |
| } | |
| $outlook = Invoke-Command -ScriptBlock { | |
| $versions = Get-ChildItem "HKLM:\SOFTWARE\WOW6432Node\Microsoft\Office" | Where-Object -Property Name -like "*.?" | Sort-Object -Property Name -Descending | |
| $script:maxLoops = $versions.Where{ $_.Name }.Count | |
| $outlookexe = $versions.ForEach({ | |
| $path = "Registry::" + $_.Name | |
| try | |
| { | |
| $versionPath = Get-ItemProperty -LiteralPath (Join-Path $path "Outlook\InstallRoot") -Name Path -ErrorAction SilentlyContinue | |
| $installRoot = ($versionPath).Path | |
| } | |
| catch {} | |
| if ($installRoot -ne $null) | |
| { | |
| $script:outlookexe = "OUTLOOK.EXE" | |
| $outlook = Join-Path $installRoot $outlookexe | |
| if (Test-Path -Path $outlookexe) | |
| { | |
| return $outlook | |
| } | |
| } | |
| }) | |
| return $outlook | |
| } | |
| $User32Definition = @" | |
| using System; | |
| using System.Runtime.InteropServices; | |
| public static class Win32 { | |
| [DllImport("user32.dll")] | |
| public static extern bool CloseWindow(IntPtr hWnd); | |
| [DllImport("user32.dll")] | |
| public static extern bool IsWindowVisible(IntPtr hWnd); | |
| [DllImport("user32.dll")] | |
| public static extern bool IsZoomed(IntPtr hWnd); | |
| } | |
| "@ | |
| # https://github.com/MicrosoftDocs/PowerShell-Docs/blob/main/reference/5.1/Microsoft.PowerShell.Utility/Add-Type.md | |
| # Add signature as new type to PowerShell (for this session) | |
| Add-Type -TypeDefinition $User32Definition -Language CSharpVersion3 | |
| Add-Type -AssemblyName System.Windows.Forms | |
| do | |
| { | |
| Write-Host "Waiting for $outlookexe to be ready, attempt: $loops of $maxLoops" -ForegroundColor Yellow | |
| Start-Sleep -Seconds 3 | |
| $outlookHandle = Get-Process OUTLOOK -ErrorAction SilentlyContinue | |
| if (-not $outlookHandle -and $autostartOutlook) | |
| { | |
| Write-Output "Starting $outlookexe from $outlook" | |
| Start-Process -FilePath $outlookexe -WindowStyle Maximized | |
| Start-Sleep -Seconds 2 | |
| [Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point(960, 60) | |
| } | |
| if ($outlookHandle) | |
| { | |
| $windowHandle = $outlookHandle.MainWindowHandle | |
| $zoomed = [Win32]::IsZoomed($windowHandle) | |
| Write-Host "$outlookexe$(if($zoomed -eq $true){' [MAX]'}) is running..." -ForegroundColor Yellow | |
| if ([Win32]::IsWindowVisible($windowHandle)) | |
| { | |
| Write-Host "$outlookexe is visible... attempting to minimize..." -ForegroundColor Yellow | |
| $minimized = [Win32]::CloseWindow($windowHandle) | |
| ##Start-Sleep -Seconds 2 | |
| if (-not $minimized) | |
| { | |
| Write-Host "Failed to minimize $outlookexe... Outlook may still be starting..." -ForegroundColor Red | |
| $outlookHandle = $null | |
| $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") | |
| exit; | |
| } | |
| elseif (-not [Win32]::IsWindowVisible($windowHandle)) | |
| { | |
| Write-Output "$outlookexe is now minimized" | |
| break; | |
| } | |
| } | |
| else | |
| { | |
| Write-Output "$outlookexe not visible..." | |
| break; | |
| } | |
| } | |
| $loops += 1 | |
| } | |
| until ($null -ne $outlookHandle -or $loops -ge $maxLoops) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment