2

I've found lots of great scripts to open a program if it isn't running, but Outlook is proving to be tricky. In Windows 10 (build 170121) it opens and runs in the background on login, so Get-Process will always find at least one instance whether the application is open from a user perspective (opens a second process). My uneducated guess is that it's always there because I set Outlook as my default mail program in Win 10 settings.

I can see unique attributes of the background process like the PID is lower and the number of User Objects = 1, but there has to be a way to definitively test if the UI is open (what I'm calling the second process and want to test for)

This is a script that works well for programs that aren't always running in the background. Would someone be kind enough to help point me to a definitive test for the UI process? Many thanks in advance!

if((Get-Process -Name OUTLOOK -ErrorAction SilentlyContinue) -eq $null){
    ."C:\Program Files\Microsoft Office\root\Office16\OUTLOOK.EXE"
}
sodawillow
  • 12,497
  • 4
  • 34
  • 44
JimBob_SF
  • 21
  • 1
  • 3
  • Thank you for the samples and to Eugene (many thanks!!!) for his note that Outlook shouldn't be doing what it is on my laptop. On my PC, Outlook isn't running the background and when I try to launch a second instance, it does indeed spit an error that only one can run at a time. My laptop is doing the impossible and I'll bang on it when I get to work. Both the PC and laptop are running the same insider build of Win 10 Ent x64 15019 on a domain and the same version of Office 2016 x64 (C2R from an Office 365 E3 subscription). – JimBob_SF Feb 01 '17 at 15:33
  • I just tested this: If Outlook (at least the 2016 x64 version) is run as Administrator, it won't allow two instances and spits a message box. If run "normally", it allows n instances. – JimBob_SF Feb 01 '17 at 15:51

3 Answers3

0

It sounds like you're half way there with the get-process part. If the process isn't running you can just start a new process.

From there you should just need to maximise and set the foreground window, this topic might help with that

Maximize window and bring it in front with powershell

Community
  • 1
  • 1
car1bo
  • 1
  • 3
0

Maybe this bit of code could help:

Add-Type -AssemblyName Microsoft.Office.Interop.Outlook
$Outlook = New-Object -ComObject Outlook.Application
if($Outlook.ActiveWindow()) { "visible" } else { "hidden" }

It does the trick here but Outlook is not running in background on my W10 system.

edit: or

try {
    [Runtime.Interopservices.Marshal]::GetActiveObject('Outlook.Application').ActiveWindow() | Out-Null
    "visible"
} catch {
    "hidden"
}
sodawillow
  • 12,497
  • 4
  • 34
  • 44
0

Only one Outlook instance can be run in the system at the same time. Outlook is a singleton.

The simplest solution is to try getting an Outlook Application instance by using the GetActiveObject method, see Obtain and Log On to an Instance of Outlook for the sample code. Then you can check out the ActiveWindow method of the Application class which returns an object representing the current Microsoft Outlook window on the desktop, either an Explorer or an Inspector object.

But sometimes you will not be able to connect to the Outlook instance if your application and Office host are run under different security contexts. You need to run both applications with the same security level (run as an administrator, for example).

The last resort is to use the FindWindow function which retrieves a handle to the top-level window whose class name and window name match the specified strings.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45