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

thank you for the script, been using this script quite some time now
recently getting the following error:

Start-Process : Cannot validate argument on parameter 'FilePath'. The argument is null or empty. Provide an argument
that is not null or empty, and then try the command again.
At C:\Users\MYUSER\Downloads\OutlookAutostartAndMinimise.ps1:63 char:23

@NotoriousPyro
Copy link
Author

NotoriousPyro commented Mar 8, 2023

Hi,

Unfortunately I don't use Outlook any more as I changed employer a long time ago. You could check the registry values, the script obtains this from:

$versions = Get-ChildItem "HKLM:\SOFTWARE\Microsoft\Office" | Where-Object { $_.Name -like '*.*' } | Sort-Object -Property Name -Descending

And then it loops through them to find the highest version:

$outlookexe = $versions.ForEach({
        $path = 'Registry::' + $_.Name
        try
        {
            $versionPath = Get-ItemProperty -LiteralPath (Join-Path $path 'Outlook\InstallRoot') -Name Path -ErrorAction SilentlyContinue
            $installRoot = ($versionPath).Path
        }
        catch {}

The empty catch is in case a version was uninstalled, it leaves an empty tree sometimes which would stop the entire thing. It's possible Microsoft changed the registry path and now your latest version is causing it to fail to obtain this path.

You could export your registry for the above path and I'll see if I can see anything obvious.

@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