0

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?

2
  • Move the function definition to the top of the script Commented Feb 5, 2017 at 21:36
  • @MathiasR.Jessen This was the solution. Can you add as answer (and please add a brief explanation) so I can accept it? Thanks! Commented Feb 5, 2017 at 21:44

1 Answer 1

0

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.