PowerShell / PowerShell

PowerShell for every system!
https://microsoft.com/PowerShell
MIT License
45.6k stars 7.31k forks source link

Start-Process: Create a new window without stealing focus #24387

Open UselessGuru opened 1 month ago

UselessGuru commented 1 month ago

Summary of the new feature / enhancement

Summary of the new feature / enhancement I would like to start a new process with start-process, but so that the new process would NOT steal focus.

start-process -WindowStyle does not have an option to do so.

https://github.com/FuzzySecurity/PowerShell-Suite/blob/master/Invoke-CreateProcess.ps1 has exactly what I am looking for.

Proposed technical implementation details (optional)

Proposed technical implementation details (optional) Add a new parameter, e.g. '-ShowWindow [nCmdShow value]' to Start-Process that takes any of the nCmdShow values listed in

https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-showwindow

similar-issues-ai[bot] commented 1 month ago

We've found some similar issues:

If any of the above are duplicates, please consider closing this issue out and adding additional context in the original issue.

Note: You can give me feedback by 👍 or 👎 this comment.

rhubarb-geek-nz commented 1 month ago

It is interesting that your code example is 9 years old, and I am sure it still works!

Now PSGallery or similar is the best place for this kind of code.

PowerShell 7 is based on dotnet (the SDK formally known as dotnet core) and for whatever reasons ProcessWindowsStyle is an enum with only four options and this enum is then used to populate lpStartupInfo.wShowWindow

Now you could add more code to add more options but it is forever a losing race...

So if this is in an external PowerShell module then

As a cross platform shell, PowerShell Core should really have had nothing to do with windowing systems, alas it is still dragging around memories of Windows 95 with Shell Execute verbs.

So yes, an excellent idea, but put it in a PSGallery module then everyone can benefit now.

UselessGuru commented 1 month ago

It is interesting that your code example is 9 years old, and I am sure it still works

Yes it still works well - all the way to the latest Win 11 24h2 :-)

Now PSGallery or similar is the best place for this kind of code

I do not want to push the code snipped to anywhere. Much more I am looking for an easy way to spawn a new process with start-process without it stealing focus.

and for whatever reasons ProcessWindowsStyle is an enum with only four options and this enum is then used to populate lpStartupInfo.wShowWindow

It is interesting to see that https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.processwindowstyle?view=net-8.0 only shows 4 values.

But https://github.com/FuzzySecurity/PowerShell-Suite/blob/master/Invoke-CreateProcess.ps1 has much more combiniations for wShowWindow:

     -ShowWindow        Window display flags:
                          0x0000 (SW_HIDE)
                          0x0001 (SW_SHOWNORMAL)
                          0x0001 (SW_NORMAL)
                          0x0002 (SW_SHOWMINIMIZED)
                          0x0003 (SW_SHOWMAXIMIZED)
                          0x0003 (SW_MAXIMIZE)
                          0x0004 (SW_SHOWNOACTIVATE)
                          0x0005 (SW_SHOW)
                          0x0006 (SW_MINIMIZE)
                          0x0007 (SW_SHOWMINNOACTIVE)
                          0x0008 (SW_SHOWNA)
                          0x0009 (SW_RESTORE)
                          0x000A (SW_SHOWDEFAULT)
                          0x000B (SW_FORCEMINIMIZE)
                          0x000B (SW_MAX)

These were probly taken from https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-showwindow

And they still work (tested on Win 11 24h2). So I was hoping that there is an easy way to pass any of these to start-process.

rhubarb-geek-nz commented 1 month ago

So I was hoping that there is an easy way to pass any of these to start-process.

There is an easy way, define another enum and another parameter and then

  1. have two parameters which control the same value (wShowWindow)

  2. define which takes precedence when both are provided

  3. when your script runs on any prior version of PowerShell it errors with

Start-Process: A parameter cannot be found that matches parameter name 'ShowWindow'.

So its more about the difference between "can" and "should", and the overall value it provides.

UselessGuru commented 1 month ago

@rhubarb-geek-nz

You speak in riddles ;-)

have two parameters which control the same value (wShowWindow) define which takes precedence when both are provided

How would I do this?

rhubarb-geek-nz commented 1 month ago

How would I do this?

I presumed you were expecting Start-Process to be changed. It already accepts

-WindowStyle <ProcessWindowStyle>

and you want it to also accept

-ShowWindow <ShowWindowStyle>

I count that as two parameters, both of which were intended to set the value of wShowWindow in the STARTUPINFO.

I was describing the coding change and the effect of that change,

UselessGuru commented 1 month ago

I presumed you were expecting Start-Process to be changed. It already accepts

I'd very much appreciate if Start-Process would get changed. However I wouldn't know how to achieve this myself. Therefore: Help!

rhubarb-geek-nz commented 1 month ago

However I wouldn't know how to achieve this myself.

Fortunately you have already take the correct first steps. This issue is marked for Triage and Issue-Enhancement so it is in the queue of things to get looked by the relevant working group. These initial discussions are also useful because they flesh out a little more detail about both expectations and possible implementation.