Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Powershell -WindowStyle Hidden still shows a window briefly #3028

Open
Ciantic opened this issue Jan 22, 2017 · 117 comments
Open

Powershell -WindowStyle Hidden still shows a window briefly #3028

Ciantic opened this issue Jan 22, 2017 · 117 comments
Labels
Issue-Enhancement the issue is more of a feature request than a bug OS-Windows WG-Interactive-Console the console experience
Milestone

Comments

@Ciantic
Copy link

Ciantic commented Jan 22, 2017

Steps to reproduce

In Windows Run dialog type this:
PowerShell.exe -WindowStyle Hidden -Command ping www.microsoft.com

Expected behavior

There should be no window, right now you can't start powershell without window flashing, making it rather useless e.g. for scheduled tasks.

Note I think this is intended behavior, but it's confusing and new option is probably required. If you search how to run a powershell in scheduled task, the go-to workaround is to do a vbs script of all things! Such as:

Dim shell,command
command = "powershell.exe -nologo -File D:\myscript.ps1"
Set shell = CreateObject("WScript.Shell")
shell.Run command,0

This is not a good thing, powershell needs this feature in the shell itself, scheduled tasks are important feature and having a window flash on scheduled task is a really bad experience.

Actual behavior

It flashes the powershell window briefly.

Environment data

> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      5.1.14393.693
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.14393.693
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
@BrucePay
Copy link
Collaborator

powershell.exe is a console application. The console window is automatically created by the OS when the process starts. The powershell.exe code that processes -WindowStyle Hidden is therefore executed after the console window is opened hence the flash. To fix this, we would need the equivalent of wscript i.e. a win32 host application instead of a console host application. 

@SteveL-MSFT SteveL-MSFT added Resolution-External The issue is caused by external component(s). Issue-Enhancement the issue is more of a feature request than a bug Up-for-Grabs Up-for-grabs issues are not high priorities, and may be opportunities for external contributors and removed Resolution-External The issue is caused by external component(s). labels Feb 2, 2017
@SteveL-MSFT
Copy link
Member

Given there's no code change we can do in powershell.exe to address this, I've changed this issue to a feature request to have a wscript-like host to support this type of scenario

@ghost
Copy link

ghost commented Feb 2, 2017

http://www.f2ko.de/en/p2e.php
and Power Shell Studio has a custom host too

@rednoah
Copy link

rednoah commented Jun 26, 2017

You could have a powershellw.exe which would be a GUI application that doesn't show a console window. Same as what javaw.exe and pythonw.exe do.

@Jackbennett
Copy link

Jackbennett commented Feb 16, 2018

Can pwsh.exe get support for this or get pwshw.exe as suggested above? With this pwsh exe being new it seems like a great time to change the behaviour of -windowstyle hidden. Nobody has ever used hidden and thought "yep that's what I wanted, flash some screen for a second".

It makes sense that powershell.exe can't be changed after all this time and its legacy.

@SteveL-MSFT
Copy link
Member

I would support a community contribution to add pwshw.exe

@gtalton
Copy link

gtalton commented Feb 21, 2018

Agree .. this would be consistant with other language executables and solve me having to currently wrap my powershell scripts in vbs scripts.

@iSazonov
Copy link
Collaborator

iSazonov commented Feb 21, 2018

Technically we could do like https://github.com/AvaloniaUI/Avalonia/wiki/Hide-console-window-for-self-contained-.NET-Core-application

editbin.exe /subsystem:windows yourapp.exe

But I wonder - if PowerShell Core is portable what is expected behavior on Unix? Can we be unified on all platforms?
Related discussion https://github.com/dotnet/cli/issues/296 It also mentions that we could use GUI subsystem.

Maybe @mklement0 have any thoughts?

@SteveL-MSFT
Copy link
Member

@iSazonov -WindowStyle isn't supported on non-Windows

@iSazonov
Copy link
Collaborator

Yes, I meant - have we the same behavior on Unix by creating a console? Have we scenarios where we don't want to create the console on Unix?

@mklement0
Copy link
Contributor

@iSazonov: I haven't really looked into this; the only thing I can tell you, from personal experience, is that invoking pwsh invisibly works fine from utilities such as Alfred 3 and TextExpander

@brice-ruppen
Copy link

What I've been using so far is a shortcut named PS with the Target: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe and the option Run: Minimized
Like this:
C:\Windows\PS Start-Process .
The taskbar does flicker but no more console.

@aresowj
Copy link

aresowj commented Aug 7, 2018

Seems like the -WindowStyle Hidden has to be the first parameter of your command line.

@MaximoTrinidad
Copy link

So, we are starting to support Windows PowerShell now??

Just a friendly reminder, if this is a PowerShell Core issue? Please provide the PowerShell Core version as is required when submitting an issues.

Otherwise, Windows PowerShell need to go thru UserVoice at: https://windowsserver.uservoice.com/forums/301869-powershell

For more information see: https://github.com/PowerShell/PowerShell#windows-powershell-vs-powershell-core

:)

@mklement0
Copy link
Contributor

@aresowj: While the placement of arguments does matter when calling PowerShell's CLI (given that anything following -Command is interpreted as part of the command to execute), (a) the OP's attempt already places it before -Command and (b) that doesn't prevent the flashing, for the reasons explained in @BrucePay's earlier comment.

@MaximoTrinidad: While this issue may have started out as focused on Windows PowerShell only, it has long since morphed into a PS Core feature request (which also deserves back-porting to Windows PowerShell).

To recap: In order to get fully invisible invocations via the PowerShell CLI:

  • On Windows, a new, separate PowerShell executable (pwshw.exe / powershellw.exe) is needed that is a GUI application, following the model of the python.exe / pythonw.exe pair

  • On macOS and Linux, where the problem doesn't exist, I presume there is no need for a separate executable. For symmetry with Windows, a simple symlink could be implemented that simply points to the regular pwsh executable.

@MaximoTrinidad
Copy link

Thanks @mklement0!
:)

@aresowj
Copy link

aresowj commented Aug 12, 2018

Sorry guys, mistook the two Powershells and thanks for your clarification. :)

@zero77
Copy link

zero77 commented Sep 3, 2018

What about initially starting Powershell in the back ground with a non visible console, then checking for any console window arguments -WindowStyle and implementing them.
But, if none are found it could then start a visible console window.

@iSazonov
Copy link
Collaborator

iSazonov commented Sep 3, 2018

@Jawz84
Copy link
Contributor

Jawz84 commented Sep 3, 2018

I created an issue in the windows console team issue repo here: microsoft/terminal#249

@mklement0
Copy link
Contributor

mklement0 commented Sep 3, 2018

@zero77:

pwsh.exe must remain a console-subsystem application to ensure synchronous, standard-streams-connected execution from an existing console window.

We therefore need the previously discussed separate, GUI-subsystem executable, pwshw.exe. If we want that executable to support conditional creation of a(n invariably new) console window, @iSazonov's link is a helpful starting point.

@Jawz84: I don't think the console team can help here:

  • A console-subsystem application such as pwsh.exe invariably creates a new console window, which happens before the application sees any of its arguments.

  • To only way to hide or prevent creation of this console window is to have a GUI-subsystem application as the entry point. In the simplest case this GUI-subsystem application can be a stub that relays arguments to the console application, but starts it hidden.

@Jawz84
Copy link
Contributor

Jawz84 commented Sep 3, 2018

I'll leave the console issue open, see what they think. I see your point, and i know it may be totally impossible. Yet then again, it may be something they would like to consider.

[Edit:] I've got the answer that this is not currently feasable to make amends for in console. A separate pwshw.exe would be the way to go for now.

@daxian-dbw
Copy link
Member

I reviewed this issue and the related Console Allocation Policy proposal again today. It looks to me will be pretty straightforward for PowerShell once the proposed Console Allocation Policy gets implemented.

PowerShell will have <consoleWindowPolicy>hidden</consoleWindowPolicy> in its fusion manifest, so that it's always attached to a headless console session when launched by Explorer, Task Scheduler, etc. At the startup, PowerShell checks if the console window is visible by IsWindowVisible,

  • If yes, then it's not the launched by a GUI Program scenario, and nothing needs to be done.
  • If not, then
    • If -WindowStyle hidden is specified, then nothing needs to be done
    • If -WindowStyle is not specified (use Normal as the default style), or it's specified with a difference value (Maximized, Minimized, and Normal), then PowerShell calls GetConsoleWindow/ShowWindow to show the hidden console.

This will keep the exact same behavior as of today, while fixing the flashing windows problem.

@iSazonov
Copy link
Collaborator

iSazonov commented Oct 7, 2021

NativeCommandProcessor sometimes allocates hidden console. Perhaps we will have to re-review this too.

@daxian-dbw
Copy link
Member

daxian-dbw commented Oct 7, 2021

Downside is obvious -- this won't work on down-level machines, so the option of pwshw.exe is still on the table unless we decide to not support this on down-level OS's.

If we decide to support pwshw.exe, then I propose to go with a similar approach as if having <consoleWindowPolicy>hidden</consoleWindowPolicy> in the fusion manifest:

  1. Always create a new hidden console session at the startup, even if pwshw is a GUI application.
  2. Make Hidden the default value for -WindowStyle, but allow user to specify other window style values for debug purpose. When a different style value is specified, show the window with that style.
  3. Requires either -c or -file flag to be present, but allow -noexit for debug purpose. When -noexit is present, show the window with Normal style, unless a different non-hidden style is specified to -WindowStyle.

With the hidden console session, all existing PowerShell script will work (yeah, won't write out or read anything, but it will not crash, and will run just like how it works today with -windowstyle hidden).

@mvelbaum
Copy link

mvelbaum commented Nov 20, 2021

Another workaround VBScript that will launch one or more scripts passed as args (great for task scheduling):

Set shell = CreateObject("WScript.Shell")
For Each arg In WScript.Arguments
    shell.Run("pwsh -windowstyle hidden -executionpolicy bypass -noninteractive -command ""&"" ""'" & arg & "'"""),0
Next

Just save as run-ps.vbs (or whatever) and then run your PS1 scripts as run-ps.vbs "path\to\myscript.ps1"
Tested with PowerShell 7 on Win 11.

@nigelwale
Copy link

How about Powershell.exe -WindowStyle VeryHidden -Command "write-host kiss"

@JohyC
Copy link

JohyC commented Dec 15, 2021

Maybe we can use vbs + bat + ps1!

ps1:
#https://github.com/songquanpeng/go-file
#i use go-file.exe in PowerShell and hidden windows!
D:\private\go-file\go-file.exe -port 80 -token private -path D:\Share

bat:
@echo off PowerShell.exe -WindowStyle Hidden -Command D:\private\go-file\go-file.ps1

vbs:
set ws=CreateObject("WScript.Shell") ws.Run "D:\private\go-file\go-file.bat",0,true

praveenkumar added a commit to praveenkumar/crc that referenced this issue Mar 28, 2022
This PR is uses windows powershell to create `crc daemon` as task
and start it on demand for the current user. This doesn't need any
elevated privillages to run `crc daemon` for user context. It try to
perform following tasks
- Check if there is already crcDaemon task
    + If present does it the latest one using version info
- Install the task using predefined xml definition
- Start the task which run the `crc daemon` in background.

There is a known issue with windows powershell when you start a task
in background using `powershell.exe` then for a second the windows pops
up and disappear, PowerShell/PowerShell#3028
have more details around it but as of now this is only the way for
interactive user context otherwise the task will need the admin
privillages which we want to avoid.
praveenkumar added a commit to praveenkumar/crc that referenced this issue Mar 28, 2022
This PR is uses windows powershell to create `crc daemon` as task
and start it on demand for the current user. This doesn't need any
elevated privillages to run `crc daemon` for user context. It try to
perform following tasks
- Check if there is already crcDaemon task
    + If present does it the latest one using version info
- Install the task using predefined xml definition
- Start the task which run the `crc daemon` in background.

There is a known issue with windows powershell when you start a task
in background using `powershell.exe` then for a second the windows pops
up and disappear, PowerShell/PowerShell#3028
have more details around it but as of now this is only the way for
interactive user context otherwise the task will need the admin
privillages which we want to avoid.
praveenkumar added a commit to praveenkumar/crc that referenced this issue Mar 28, 2022
This PR is uses windows powershell to create `crc daemon` as task
and start it on demand for the current user. This doesn't need any
elevated privillages to run `crc daemon` for user context. It try to
perform following tasks
- Check if there is already crcDaemon task
    + If present does it the latest one using version info
- Install the task using predefined xml definition
- Start the task which run the `crc daemon` in background.

There is a known issue with windows powershell when you start a task
in background using `powershell.exe` then for a second the windows pops
up and disappear, PowerShell/PowerShell#3028
have more details around it but as of now this is only the way for
interactive user context otherwise the task will need the admin
privillages which we want to avoid.
praveenkumar added a commit to praveenkumar/crc that referenced this issue Mar 28, 2022
This PR is uses windows powershell to create `crc daemon` as task
and start it on demand for the current user. This doesn't need any
elevated privillages to run `crc daemon` for user context. It try to
perform following tasks
- Check if there is already crcDaemon task
    + If present does it the latest one using version info
- Install the task using predefined xml definition
- Start the task which run the `crc daemon` in background.

There is a known issue with windows powershell when you start a task
in background using `powershell.exe` then for a second the windows pops
up and disappear, PowerShell/PowerShell#3028
have more details around it but as of now this is only the way for
interactive user context otherwise the task will need the admin
privillages which we want to avoid.
praveenkumar added a commit to praveenkumar/crc that referenced this issue Mar 29, 2022
This PR is uses windows powershell to create `crc daemon` as task
and start it on demand for the current user. This doesn't need any
elevated privillages to run `crc daemon` for user context. It try to
perform following tasks
- Check if there is already crcDaemon task
    + If present does it the latest one using version info
- Install the task using predefined xml definition
- Start the task which run the `crc daemon` in background.

There is a known issue with windows powershell when you start a task
in background using `powershell.exe` then for a second the windows pops
up and disappear, PowerShell/PowerShell#3028
have more details around it but as of now this is only the way for
interactive user context otherwise the task will need the admin
privillages which we want to avoid.
praveenkumar added a commit to praveenkumar/crc that referenced this issue Mar 31, 2022
This PR is uses windows powershell to create `crc daemon` as task
and start it on demand for the current user. This doesn't need any
elevated privillages to run `crc daemon` for user context. It try to
perform following tasks
- Check if there is already crcDaemon task
    + If present does it the latest one using version info
- Install the task using predefined xml definition
- Start the task which run the `crc daemon` in background.

There is a known issue with windows powershell when you start a task
in background using `powershell.exe` then for a second the windows pops
up and disappear, PowerShell/PowerShell#3028
have more details around it but as of now this is only the way for
interactive user context otherwise the task will need the admin
privillages which we want to avoid.
praveenkumar added a commit to crc-org/crc that referenced this issue Mar 31, 2022
This PR is uses windows powershell to create `crc daemon` as task
and start it on demand for the current user. This doesn't need any
elevated privillages to run `crc daemon` for user context. It try to
perform following tasks
- Check if there is already crcDaemon task
    + If present does it the latest one using version info
- Install the task using predefined xml definition
- Start the task which run the `crc daemon` in background.

There is a known issue with windows powershell when you start a task
in background using `powershell.exe` then for a second the windows pops
up and disappear, PowerShell/PowerShell#3028
have more details around it but as of now this is only the way for
interactive user context otherwise the task will need the admin
privillages which we want to avoid.
@stax76
Copy link

stax76 commented Aug 4, 2022

I ported run-hidden from C# to C++, so it's a tiny bit faster:

https://github.com/stax76/run-hidden

GitHub
run-hidden runs Windows console apps like PowerShell with hidden console window. - GitHub - stax76/run-hidden: run-hidden runs Windows console apps like PowerShell with hidden console window.

@ayyyiieneheuihues
Copy link

Hi,

Just adding my 2 cents here. This bug doesn't just occur when you run the Windows style Hidden argument in a run dialog but also through any task called by the Windows Task Scheduler. For example certbot's auto renew is a task that runs twice a day with this argument and has the PowerShell window briefly flash.

It's a really spooky bug if you don't know what's going on. I thought I had a virus because PowerShell would flash randomly. I'm not the only one who has thought this because when I searched PowerShell flashing randomly I found a Stack Overflow post mentioning to check the Task Scheduler. It's one thing to see the Powershell window flash briefly after you entered a command in the run dialog and hit enter. It's another thing when you're just using your computer normally and this just happens randomly.

@Roelovic
Copy link

if you want to run this when user is logged in without highest privileges. just dont use the arguments box. put everything immediately after powershell.exe and make sure that -windowstyle hidden is first.
image

@pluma9
Copy link

pluma9 commented Mar 17, 2024

I know this is already implied in @Roelovic 's comment, but I just want to emphasize that the method won't work for commands, e.g. Register-ScheduledTask ... -Action (New-ScheduledTaskAction -Execute pwsh -Argument "-windowstyle hidden -no-profile -file script.ps1")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue-Enhancement the issue is more of a feature request than a bug OS-Windows WG-Interactive-Console the console experience
Projects
None yet