How do I avoid PShell window when I run a ps1 script w/Task Scheduler?

Page 1 of 5 123 ... LastLast

  1. Posts : 46
    Win 10 Pro x64 21H2
       #1

    How do I avoid PShell window when I run a ps1 script w/Task Scheduler?


    I’ve created a script intended to support a backup cycle by playing a sound and displaying a window with relevant info which is executed via Task Scheduler. When I test run the script I either get
    1. the alert window/sound plus the PowerShell window when "Run only when user is logged on" is selected, or
    2. only sound and no alert/PowerShell windows if "Run whether user is logged on or not" is selected


    The desired state is having the alert window displayed but not the PowerShell window. I’ve tried using the run “Hidden” option but that didn’t do the trick.

    How do I get the alert window without the PowerShell window?

    Thanx in advance for your time & expertise.
      My Computer


  2. Posts : 43,013
    Win 10 Pro (22H2) (2nd PC is 22H2)
       #2

    You might try
    How to run a powershell script silently

    unless that's exactly what you've already done. (I've not tried it myself)
      My Computers


  3. Posts : 46
    Win 10 Pro x64 21H2
    Thread Starter
       #3

    dalchina said:
    Thanks for the starter, it nearly worked. Task Scheduler is a bit persnickety tho:
    How do I avoid PShell window when I run a ps1 script w/Task Scheduler?-reflect-available-space-alert-ts.png


    This does not entirely suppress displaying the PS window . It flashes the PS window border both with a test and a scheduled run. It happened very quickly but it looks like it drew the border top left --> top right --> bottom right --> top left then disappeared before drawing the blue background. Ah well, it will certainly grab my attention...

    Marking as solved and thanx again.
      My Computer


  4. Posts : 16,956
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #4

    ritjesman said:
    only sound and no alert/PowerShell windows if "Run whether user is logged on or not" is selected
    You can never use that TS setting for anything that has a user interface.

    ritjesman said:
    The desired state is having the alert window displayed but not the PowerShell window. I’ve tried using the run “Hidden” option but that didn’t do the trick.
    ritjesman said:
    How do I get the alert window without the PowerShell window?
    The solution I adopt for batch files is not to run them directly from TS but to use TS to trigger a vbs intermediary that starts the batch file [normally, minimised, hidden - as required]

    So the TS task action would be
    How do I avoid PShell window when I run a ps1 script w/Task Scheduler?-ts-task-vbs-intermediary.png
    In your case, the argument would be "D:\Computer\Scripts\StartBU_StorSpaceStat.vbs"

    The vbs file would be this in order to run the PS1 hidden
    StartBU_StorSpaceStat.vbs
    Code:
    Set WshShell = CreateObject("WScript.Shell")
    WshShell.Run "Powershell.exe -File " & chr(34) & "D:\Computer\Scripts\BU_StorSpaceStat.ps1" & Chr(34), 0
    Set WshShell = Nothing

    If I wanted the PS1 to run minimised instead, I'd use
    StartBU_StorSpaceStat.vbs
    Code:
    Set WshShell = CreateObject("WScript.Shell")
    WshShell.Run "Powershell.exe -File " & chr(34) & "D:\Computer\Scripts\BU_StorSpaceStat.ps1" & Chr(34),  7
    Set WshShell = Nothing

    I normally point my vbs intermediaries to a shortcut to the script instead of the script itself. That allows me to set a specific icon to appear in the Taskbar. I always set the shortcut to run Minimised even if I'm going to use the vbs intermediary to hide it completely - I just find that this eases testing the whole thing.
    Note that, for a ps1 file, you make a normal shortcut then add
    powershell.exe -file
    in the front of the Target field.
    - It would otherwise just open the ps1 in Notepad instead of running it.

    If I wanted the PS1 to run hidden yet refer to the shortcut, I'd use
    StartBU_StorSpaceStat.vbs
    Code:
    Set WshShell = CreateObject("WScript.Shell")
    WshShell.Run chr(34) & "D:\Computer\Scripts\BU_StorSpaceStat.lnk" & Chr(34), 0
    Set WshShell = Nothing
    or, finally, to run the PS1 minimised whilst referring to the lnk to get the icon, I'd use
    StartBU_StorSpaceStat.vbs
    Code:
    Set WshShell = CreateObject("WScript.Shell")
    WshShell.Run chr(34) & "D:\Computer\Scripts\BU_StorSpaceStat.lnk" & Chr(34), 7
    Set WshShell = Nothing

    These are my batch file notes on the subject. I have yet to add the ps1 variations [above] to it.
    Make Task scheduler run a batch file minimised and with a specific icon - TenForums
    but that icon applies only to the batch file or PS1 window itself not to any dialog that it generates. Dialogs have their own Taskbar icons generated by their own code.

    That article also contains a guide to protecting the folders used to house scripts. In my case, C:\Tools & its subfolders and in yours D:\Computer\Scripts

    Denis
    Last edited by Try3; 18 Nov 2020 at 05:21.
      My Computer


  5. Posts : 43,013
    Win 10 Pro (22H2) (2nd PC is 22H2)
       #5

    That said it's certainly possible to do that completely silently with bat files.
      My Computers


  6. Posts : 16,956
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #6

    I have corrected the vbs code in post #4 for running PS1 and for running a shortcut to a PS1.

    I have tested this all the way through from running a TS task and there is no flash of a window or its outline at all.

    Personally, I always run them minimised instead of hidden so that I can see it running so I have included this option in the vbs code above.

    Denis
      My Computer


  7. Posts : 7,607
    Windows 10 Home 20H2
       #7

    I am afraid the VBScript phobic will come to dissuade the OP.
      My Computer


  8. Posts : 46
    Win 10 Pro x64 21H2
    Thread Starter
       #8

    Matthew Wai said:
    I am afraid the VBScript phobic will come to dissuade the OP.
    it did initially until I read down to Try3's post re: having tested it through & realized I should be
    1. setting up a TS task to execute the generously provided vbs script (which presumably avoids activating a GUI like the PS window)
    2. use the "Start..." script to kickoff the ps1


    Now I feel compelled to figure out how to use the VBScript I select ... I shall return
      My Computer


  9. Posts : 7,607
    Windows 10 Home 20H2
       #9

    Try3 is the one you can always trust. I use VBScript every day.
    Looking forward to your return.
      My Computer


  10. Posts : 16,956
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #10

    ritjesman said:
    which presumably avoids activating a GUI like the PS window
    The method I've provided will indeed avoid that flash of a PS window appearing.
    Task scheduler runs the vbs. The vbs starts the PS1. The PS1 plays a sound and displays a window.

    Don't forget the other point I made as it is also essential - that is
    ritjesman said:
    ... playing a sound and displaying a window ...
    ritjesman said:
    only sound and no alert/PowerShell windows if "Run whether user is logged on or not" is selected
    Try3 said:
    You can never use that TS setting for anything that has a user interface.
    That "window" you want to display is a user interface so you cannot use the "Run whether user is logged on or not" option.

    Denis
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 03:24.
Find Us




Windows 10 Forums