Skip to content

Instantly share code, notes, and snippets.

@NotoriousPyro
Last active March 19, 2023 11:13
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NotoriousPyro/d30a96d2a89cf783ef3081b13d4816a0 to your computer and use it in GitHub Desktop.
Save NotoriousPyro/d30a96d2a89cf783ef3081b13d4816a0 to your computer and use it in GitHub Desktop.
Automatically finds, runs and places Outlook into the system tray.
<#
.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)
#>
$autostartOutlook = $true
$outlook = Invoke-Command -ScriptBlock {
$versions = Get-ChildItem "HKLM:\SOFTWARE\Microsoft\Office" | Where-Object { $_.Name -like '*.*' } | Sort-Object -Property Name -Descending
$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)
{
$outlookexe = Join-Path $installRoot 'OUTLOOK.EXE'
if (Test-Path -Path $outlookexe)
{
return $outlookexe
}
}
})
return $outlookexe
}
$User32Definition = @'
[DllImport("user32.dll")] public static extern bool CloseWindow(IntPtr hWnd);
[DllImport("user32.dll")] public static extern bool IsWindowVisible(IntPtr hWnd);
'@
# add signature as new type to PowerShell (for this session)
$User32 = Add-Type -MemberDefinition $User32Definition -Name User32 -PassThru
$maxLoops = 10; $loops = 0
do
{
$outlookHandle = Get-Process OUTLOOK -ErrorAction SilentlyContinue
if (-not $outlookHandle -and $autostartOutlook)
{
Write-Verbose "Starting OUTLOOK.EXE from $outlook"
Start-Process $outlook
Start-Sleep -Seconds 2
}
if ($outlookHandle)
{
$windowHandle = $outlookHandle.MainWindowHandle
Write-Verbose "OUTLOOK.EXE is running..."
if ($User32::IsWindowVisible($windowHandle))
{
Write-Verbose "OUTLOOK.EXE is visible... attempting to minimize..."
$minimized = $User32::CloseWindow($windowHandle)
Start-Sleep -Seconds 2
if (-not $minimized)
{
Write-Verbose "Failed to minimize OUTLOOK.EXE... Outlook may still be starting..."
$outlookHandle = $null
}
elseif (-not $User32::IsWindowVisible($windowHandle))
{
Write-Verbose "OUTLOOK.EXE is now minimized"
break;
}
}
else
{
Write-Verbose "OUTLOOK.EXE not visible..."
break;
}
}
Write-Verbose "Waiting for OUTLOOK.EXE to be ready, attempt: $loops of $maxLoops"
Start-Sleep -Seconds 2
$loops += 1
}
until ($null -ne $outlookHandle -or $loops -ge $maxLoops)
@kamilmirza
Copy link

well I have hardcoded the OUTLOOK.EXE path in Line#19
it working fine
thank you again for this neat script 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment