Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a PowerShell script without displaying a window?

People also ask

How do I Run a PowerShell script without opening the window?

Copy PowerShell.exe -windowstyle hidden { Script you want to execute.. } PowerShell execution of the above command is shown in the below image. Once entered, the window disappears, yet the application would be running in the background.

How do I Run a PowerShell script without closing the window?

PowerShell NoExit switch prevents the PowerShell console window from closing after running the script. When you double click on the PowerShell script file, or run with the PowerShell option, the script will execute quickly and then disappear.

How do I Run a PowerShell script in the background?

Start-Job uses the ScriptBlock parameter to run Get-Process as a background job. The Name parameter specifies to find PowerShell processes, pwsh . The job information is displayed and PowerShell returns to a prompt while the job runs in the background. To view the job's output, use the Receive-Job cmdlet.

How do I make a window transparent in PowerShell?

In the Windows PowerShell Properties window, go to the Colors tab. There, find the slider for Opacity, and move it to the desired value. The app window changes its opacity in real time and becomes transparent to the level you set. Find the desired opacity level, and then press OK to save your changes.


You can either run it like this (but this shows a windows for a while):

PowerShell.exe -windowstyle hidden { your script.. }

Or you use a helper file I created to avoid the window called PsRun.exe that does exactly that. You can download source and exe file Run scheduled tasks with WinForm GUI in PowerShell. I use it for scheduled tasks.

Edited: as Marco noted this -windowstyle parameter is available only for V2.


I was having this same issue. I found out if you go to the Task in Task Scheduler that is running the powershell.exe script, you can click "Run whether user is logged on or not" and that will never show the powershell window when the task runs.


You can use the PowerShell Community Extensions and do this:

start-process PowerShell.exe -arg $pwd\foo.ps1 -WindowStyle Hidden

You can also do this with VBScript: http://blog.sapien.com/index.php/2006/12/26/more-fun-with-scheduled-powershell/

  • Schedule Hidden PowerShell Tasks (Internet Archive)

  • More fun with scheduled PowerShell (Internet Archive)

(Via this forum thread.)


Here's an approach that that doesn't require command line args or a separate launcher. It's not completely invisible because a window does show momentarily at startup. But it then quickly vanishes. Where that's OK, this is, I think, the easiest approach if you want to launch your script by double-clicking in explorer, or via a Start menu shortcut (including, of course the Startup submenu). And I like that it's part of the code of the script itself, not something external.

Put this at the front of your script:

$t = '[DllImport("user32.dll")] public static extern bool ShowWindow(int handle, int state);'
add-type -name win -member $t -namespace native
[native.win]::ShowWindow(([System.Diagnostics.Process]::GetCurrentProcess() | Get-Process).MainWindowHandle, 0)

Here's a one-liner:

mshta vbscript:Execute("CreateObject(""Wscript.Shell"").Run ""powershell -NoLogo -Command """"& 'C:\Example Path That Has Spaces\My Script.ps1'"""""", 0 : window.close")

Although it's possible for this to flash a window very briefly, that should be a rare occurrence.


ps1 hidden from the Task Scheduler and shortcut too

    mshta vbscript:Execute("CreateObject(""WScript.Shell"").Run ""powershell -ExecutionPolicy Bypass & 'C:\PATH\NAME.ps1'"", 0:close")