How to run a PowerShell script from Windows Task Scheduler?

I am trying to get Windows Task Scheduler to run a PowerShell script. I have a script file with the following code.

[System.Windows.MessageBox]::Show('Hello')

This script runs fine when I run it using the PowerShell program. I’ve tried to run it with Task Scheduler on Windows using the instructions in this article. How to run PowerShell scripts from Task Scheduler But I just get a blue window that closes quickly with red text that I can’t read before it closes. How do I run this script from Task Scheduler?

Have you added

Add-Type -AssemblyName PresentationFramework

at the top of the script?

I just tried that. It doesn’t change it, I get the same thing.

So you may add temporarily the parameter -NoExit to the “Add Arguments” section of the task scheduler and maybe add a Start-Sleep at the end of your script. So you can see the error message.

Okay. I got this message:

The string is missing the terminator: '.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : TerminatorExpectedAtEndOfString

That’s the whole error message? Could you maybe share the code you’re trying to run?

It’s the same as in my first post, with the addition of the line Matt suggested. Here’s the full script file as of now.

Add-Type -AssemblyName PresentationFramework
[System.Windows.MessageBox]::Show('Hello')

Works fine for me.

If you’re sure there’s nothing else in the script file, please share what you’ve got in the Action settings for the scheduled task.

… just tested it … works for me too. :wink:

Like I said, it works fine for me when I run the script from PowerShell. Here’s my edit action page.

And here’s the text of my “add arguments” box:

-ExecutionPolicy Bypass -NoExit C:\Users\Corey's Windows PC\Documents\PowerShellScripts\test_script.ps1

You should put -File in front of your file path and you have to wrap the path in double quotes.

1 Like

That worked. Thanks.