Batch script being run in background, launched by a service, does not show any of the GUI sections of the script, how to work around?

George Marantos 0 Reputation points
2025-03-21T07:43:41.5366667+00:00

I have a batch script, that is launched by a service, hence the script runs in the background.
In the script I have a notification using BurntToast which is launched like this:

:: Check if the app icon exists
if exist "%APP_ICON_PATH%" (
    set "PS_COMMAND=New-BurntToastNotification -AppLogo '%APP_ICON_PATH%' -Text '%TITLE%', '%MESSAGE%'"
) else (
    set "PS_COMMAND=New-BurntToastNotification -Text '%TITLE%', '%MESSAGE%'"
)

powershell -executionpolicy bypass -Command "%PS_COMMAND%"

When I run the script manually in a powershell terminal, it works fine and the notification pops up, however as soon as the script runs in the background the notification doesn't pop up.
How would I solve this, I would still like the script to be launched the same way, is there any way I can change the script that would solve this issue?

Thanks in advance!

PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,909 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Molly Lu-MSFT 2,586 Reputation points Microsoft External Staff
    2025-03-21T08:49:16.3333333+00:00

    Hello,

    Thank you for posting in Microsoft Q&A.

    Based on the description, I understand your question is related to Batch script.

    Go to Settings > System > Notifications & actions.

    Make sure notifications are enabled for PowerShell. Scroll down to find Windows PowerShell and ensure that Show notification banners and Show notifications in action center are both enabled.

    Check the script runs in the same user session as the logged-in user. Run Task Scheduler to run the script with the option Run only when user is logged on.

    Check Focus Assist is not blocking notifications. Focus Assist settings can be found under Settings > System > Focus assist. Make sure it is set to Off or configured to allow notifications.

    Have a nice day.

    Best Regards,

    Molly

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it

    0 comments No comments

  2. MotoX80 35,716 Reputation points
    2025-03-21T13:33:29.68+00:00

    In the toast notification scripts that I have, I see this.

    # Import the BurntToast module for creating toast notifications
    Import-Module BurntToast
    

    I suspect that the module has been imported/installed into the PS session for your account, but since the service is likely running as the system account, then it doesn't know anything about New-BurntToastNotification.

    You can verify that by capturing stdout/stderr from powershell.exe.

    :: Check if the app icon exists
    if exist "%APP_ICON_PATH%" (
        set "PS_COMMAND=New-BurntToastNotification -AppLogo '%APP_ICON_PATH%' -Text '%TITLE%', '%MESSAGE%'"
    ) else (
        set "PS_COMMAND=New-BurntToastNotification -Text '%TITLE%', '%MESSAGE%'"
    )
    powershell -executionpolicy bypass -Command "%PS_COMMAND%"  1>c:\Scripts\Logs\Toaster.log 2>&1
    

    If that's the problem, then you can install the module for all users.

    See the section "Installing Modules for all Users in Program Files" in this link.

    https://learn.microsoft.com/en-us/powershell/scripting/developer/module/installing-a-powershell-module?view=powershell-5.1

    https://www.comparitech.com/net-admin/install-powershell-modules/

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.