1

The answers to this question address the first part of my question.

Unfortunately when I schedule a job such that the powershell window is hidden, any toast notifications that my script sends are not shown. The script shows toast notifications using this script (source):

Add-Type -AssemblyName System.Windows.Forms 
$global:balloon = New-Object System.Windows.Forms.NotifyIcon
$path = (Get-Process -id $pid).Path
$balloon.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($path) 
$balloon.BalloonTipIcon = [System.Windows.Forms.ToolTipIcon]::Warning 
$balloon.BalloonTipText = 'What do you think of this balloon tip?'
$balloon.BalloonTipTitle = "Attention $Env:USERNAME" 
$balloon.Visible = $true 
$balloon.ShowBalloonTip(5000)

When I schedule the powershell script in a way that it's not hidden, notifications are shown, but every time the script is triggered I briefly see a powershell window.

How can I schedule a task or job that hides the powershell window, but shows toast notifications?

Saaru Lindestøkke
  • 2,067
  • 1
  • 25
  • 51
  • This may contain what you are looking for: https://stackoverflow.com/questions/46808635/how-to-run-powershell-script-using-task-scheduler-in-silent-hidden-mode – Thom Schumacher Feb 26 '20 at 15:48

2 Answers2

4

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. I like to use VB script to start PowerShell to ensure a completely hidden experience

In, say ps-run_hidden.vbs put

Set objShell = CreateObject("Wscript.Shell")
Set args = Wscript.Arguments
For Each arg In args
    objShell.Run("powershell -windowstyle hidden -executionpolicy bypass -noninteractive ""&"" ""'" & arg & "'"""),0
Next

Then use it to run the command you want, e.g. from Windows' scheduled tasks like so

wscript "C:\Path\To\ps-run_hidden.vbs" "C:\Other\Path\To\your-script.ps1"

You can now run a task without seeing any flashing windows.

jfrmilner
  • 1,458
  • 10
  • 11
1

You can run it as a Job in the background that way no window pops up and you get your message displayed,

Note: Any variables declared outside of Start-Job will need to be used like $using:variable. If $pid is declared outside of the Start-Job, you will use $using:pid.

Start-Job -ScriptBlock { 
    Add-Type -AssemblyName System.Windows.Forms 
    $global:balloon = New-Object System.Windows.Forms.NotifyIcon
    $path = (Get-Process -id $using:pid).Path
    $balloon.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($path) 
    $balloon.BalloonTipIcon = [System.Windows.Forms.ToolTipIcon]::Warning 
    $balloon.BalloonTipText = 'What do you think of this balloon tip?'
    $balloon.BalloonTipTitle = "Attention $Env:USERNAME" 
    $balloon.Visible = $true 
    $balloon.ShowBalloonTip(5000)
} | Wait-Job | Remove-Job

Once you have the above code in your ps1 file, simply call the following line. It will not display powershell window and provide you with the notification window.

powershell -File myScript.ps1 -WindowStyle Hidden
Jawad
  • 11,028
  • 3
  • 24
  • 37
  • 1
    This is not hiding the start-phase of the powershell-process. It will pop-up for some milliseconds before it reads the WindowStyle-attribute. – Carsten Oct 08 '20 at 08:42