PowerShell in Task Manager Shows Window
Asked Answered
O

2

2

I am trying to make Task Schedule Task so it is completely invisible that a PowerShell script is running so I created a Task on my Win10 machine configured as follows:

Program/Script:

powershell.exe

Add arguments (optional):

-WindowStyle Hidden -command "& {Out-File 'C:\temp\somefile.txt'}" -NonInteractive -NoLogo -NoProfile

When I run this task the powershell command windows pops up for a split second which I don't want.

Okoka answered 22/6, 2018 at 15:12 Comment(2)
it appears to be a known issue? github.com/PowerShell/PowerShell/issues/3028Genoese
in the comment above one of the users suggests start-process: 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.Spenserian
E
4

You can get around this with an 'Application Host' type wrapper. This is a known issue with powershell as a console-based host.

The most convenient way to do this I've found, is to use WScript.exe and run a VBS script that will invoke the process "invisibly", with no console or task bar flicker.

VBS Code:

On Error Resume Next

ReDim args(WScript.Arguments.Count-1)

For i = 0 To WScript.Arguments.Count-1
    If InStr(WScript.Arguments(i), " ") > 0 Then
        args(i) = Chr(34) & WScript.Arguments(i) & Chr(34)
    Else
        args(i) = WScript.Arguments(i)
        End If

Next

CreateObject("WScript.Shell").Run Join(args, " "), 0, False

Save the above code in a file with extension '.vbs', and place it somewhere it can be run by the client running the task. This may be in a protected fileshare on the network (if you expect the script it invokes to only run while connected to the network), or locally on the client.

Now when you invoke your console-based script (PowerShell, BAT, CScript, etc.), you invoke this VBS script with WScript explicitly WScript.exe. It also pays to throw on the 'Batch Mode' parameter //B which will suppress script errors & prompts - such as if the wrapper file itself can't be found.

At this point, all you need to do is pass powershell & the command you want powershell to run to this launch sequence:

 WScript.exe //B "\\Path\To\Launcher.VBS" powershell.exe -ExecutionPolicy ByPass -file "\\Powershell\Script\To\Run"
Ermeena answered 24/6, 2018 at 7:26 Comment(3)
I've been looking up solutions for a while now but yours is the only one that worked for my setup! Thanks a bunch. If anyone is interested, I use @Adam 's answer to run the following: WScript.exe //B "W:\My Documents\invisiblelauncher.VBS" powershell.exe -NoLogo -NonInteractive -ExecutionPolicy AllSigned -WindowStyle Hidden -Command "(New-Object -ComObject Shell.Application).ToggleDesktop()" which is the "Show Desktop" Function (same as Win+D).Hepatitis
Is there a way to put all the VBS in one line as an argument for wscript.exe, so that it doesn't require having a separate .vbs script saved somewhere?Underwent
No, unfortunately not.Ermeena
D
0

I had the same problem, it was resolved at the simple way.

When you create a Task on Windows, just set this configuration:

  1. Open Properties dialog;
  2. Then you check Run whether user is logged on or not;
  3. You can check Do not store password to avoid asking for PC password on Task execution;

In Add arguments (optional): just:

-File 'C:\temp\somefile.txt

This spcript will run without popup the prompt.

Solved in this link below:

https://mcmap.net/q/116817/-how-to-run-a-powershell-script-without-displaying-a-window

Dniester answered 11/9, 2022 at 18:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.