1

I have a powershell script that is in my startup:

%appdata%\Microsoft\Windows\Start Menu\Programs\Startup\

Note, the script itself is not in startup. Rather in \Startup\ is a shortcut to the script. The shortcut is to:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -NoProfile -File C:\develop\utils\powershell\converter.ps1

and it sits in a loop all day, waiting for events. This is the loop:

try {
    # At the end of the script... 
    while ($true) {
        Start-Sleep -Seconds .2
    }    
}
catch {}
Finally {
    # Work with CTRL + C exit too !
    Unregister-Event -SourceIdentifier $eventname 
}

Works great.

But when I log in, there is that command box....

How do I get the script to be exactly what it is, but minimized? (Or even better,,, sit in the tray... but only if that is easy! What I mean here is: Ideally it wouldn't even be in my alt-tab window sequence... but I could terminate it if I needed to....)

I should further note that the script has to be in my user session, as the events it feeds from are triggered by me as I use other apps. (IOW: Putting it in task scheduler is not an option.)

Jonesome Reinstate Monica
  • 6,618
  • 11
  • 65
  • 112
  • 1
    Log in? To where? How is the script being called/ran? – Abraham Zinala Mar 09 '22 at 15:52
  • 1
    do you have your PS1s associated to PowerShell/Pwsh.exe? Does it really run just placing your script in that location? I only ask since .ps1's are associate with text files by default. Could you maybe create a shortcut to your ps1 (*place your ps1 elsewhere*), place the shortcut it in that location, and call on your ps1 from the shortcut using: `powershell.exe -WindowStyle Hidden ....`? – Abraham Zinala Mar 09 '22 at 16:28
  • 1
    @AbrahamZinala Thank you! OP enhanced to engage your question. (And thank you for pushing me to be more detailed.) – Jonesome Reinstate Monica Mar 09 '22 at 16:38

1 Answers1

3

Update: The following instructions work as advertised, but you state that you were aware of this approach, and that your only problem was that the styling of File Explorer's GUI in Windows 11 may falsely suggest that the window-style dropdown list (field Run in a shortcut file's Properties dialog) is disabled.


Here's how to run your script minimized on startup, via the user-specific "$env:APPDATA\Microsoft\Windows\Start Menu\Programs\Startup" folder.

  • Move your script file, say foo.ps1 out of the startup folder,[1] say to your desktop.

  • In its place, create a shortcut file (.lnk) that calls your script file with its window minimized:

    • To do this interactively, via File Explorer's New > Shortcut shortcut-menu command:
      • Paste the following target command and submit:

        powershell.exe -ExecutionPolicy Bypass -File "%USERPROFILE%\Desktop\foo.ps1"  
        
      • Type a file name (.lnk will be appended, though that extension is by default hidden in File Explorer) and submit.

      • Open the Properties dialog for the newly created file and, in field Run, select Minimized, then submit.


[1] As Abraham Zinala points out, .ps1 files are not executed by default when placed in the startup folder or double-clicked in File Explorer (instead, they are opened for editing). However, it is possible to reconfigure a system to do that - see this answer. Still, that wouldn't give you control over the script's window state when placed directly in the startup folder.

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • Man, I know about that option. But on Windows11, the explorer GUI is messed up. They made too many things understated. (Like alt-tab, the highlight is too faint.) I looked on the shortcut properties, but the Win11 styling has the combobox selector so understated that it appears to be disabled. – Jonesome Reinstate Monica Mar 09 '22 at 16:41
  • Understood, @JonesomeReinstateMonica - I've added a clarification at the top of the answer. – mklement0 Mar 09 '22 at 16:53