5

It seems that:

Start-Process -WindowStyle Maximized 'C:\Program Files\Microsoft Office\root\Office16\ONENOTE.EXE'

starts OneNote in a window style regardless the parameter is Maximized or Minimized:

  • If the last time is normal or maximized, then it will open normal or maximized
  • If the last time is minimized, then it will open normal

Do you know why? I have version 6 on my Windows 7 32-bit machine.


From Windows Docs: Start-Process

Ooker
  • 1,969
  • 4
  • 28
  • 58
  • 1
    Unfortunately, there's no guarantee that the target program will honor the specified window style, and that is what seems to be happening here. – mklement0 Nov 22 '18 at 04:36
  • @OwainEsau thanks. Do you know why it works? I visit the registry and see the value is already 1, but the normal method still open it normal. And what about minimized? – Ooker Nov 22 '18 at 05:44
  • @mklement0 how to know which program will honor the style? – Ooker Nov 22 '18 at 05:44
  • Might need to refresh the registry, for me it changes after you close onenote. This just sets the window size. I would assume that this registry key causes the `-WindowStyle` parameter to be ignored – Owain Esau Nov 22 '18 at 05:46
  • 1
    @OwainEsau I think you can make it as an answer. But yes, I think this only works for certain programs, because some don't have register keys – Ooker Nov 22 '18 at 05:51
  • 1
    @Ooker: I'm glad Owain came up with a solution. As for finding out in general whether a given application honors the requested window style: I don't think you'll find that documented, so trial & error is the only way to find out. – mklement0 Nov 22 '18 at 12:41
  • @mklement0 but think about it, this also means that such program must save the setting somewhere, and must have a strong reason to override the request. So the question is: why? – Ooker Nov 22 '18 at 13:24
  • 2
    @Ooker: As for why: It's helpful for applications to allow you to persistently configure the desired startup window state or to automatically restore the last session's window state. The problem is that the application itself won't know that an explicit startup window state was requested via `Start-Process -WindowStyle`, so it can't know when to override. In short: you need to use the respective app's _custom_ mechanism, as in this case - unfortunately, there's no standardized way. Sadly, there are also apps that simply ignore the requested state, despite not offering a custom mechanism. – mklement0 Nov 22 '18 at 14:55

2 Answers2

3

Simply passing the value Maximized to the -WindowStyle parameter isn't enough. You need to use the fully qualified enum Field or value.

i.e.

Start-Process "C:\Program Files\Microsoft Office\root\Office16\ONENOTE.EXE" -WindowStyle ([System.Diagnostics.ProcessWindowStyle]::Maximized)

OR

Start-Process "C:\Program Files\Microsoft Office\root\Office16\ONENOTE.EXE" -ArgumentList ("-NoExit") -WindowStyle (3)

The ProcessWindowSyle enums are defined here: https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.processwindowstyle?view=netframework-1.1

youzer
  • 493
  • 4
  • 11
1

As mklement0 stated, there is no guarantee that the program will honour the requested window style.

You can in some cases get around this by editing a relevant registry key. In this case:

$registryPath = "HKCU:\Software\Microsoft\Office\16.0\OneNote\General\"; 
Set-ItemProperty -path $registryPath -name "WindowMode" -Value 1; 
Start-Process 'C:\Program Files (x86)\Microsoft Office\root\Office16\ONENOTE.EXE'
Owain Esau
  • 1,876
  • 2
  • 21
  • 34
  • I open a question to find other workarounds like this: [Where may setting about last opened window style be stored?](https://superuser.com/q/1377823/301042) – Ooker Nov 23 '18 at 14:47