0
votes

I have the following simple PowerShell script:

foo "Hello World!"
Start-Sleep 5

function foo($message) {
    write-host $message
} 

This works correctly when I run from the ISE. However, when I try to add this as a scheduled task in Windows I get the following error:

foo : The term 'foo' is not recognized as the name of a cmdlet, function, script file, or operable program.

How can I fix the script to allow it to run as a Windows Scheduled Task?

1
Move the function definition to the top of the script - Mathias R. Jessen
@MathiasR.Jessen This was the solution. Can you add as answer (and please add a brief explanation) so I can accept it? Thanks! - Mike Cole

1 Answers

0
votes

define the function first. You'll find it will run the 2nd time in the ISE, because of they way the shell remembers. Define the function 1st:

function foo($message) {
    write-host $message
} 
foo "Hello World!"
Start-Sleep 5