Why functions are not available locally until after first ran?

Question:

I have two questions here, why does the following function in a script not recognized when I run the script:

Script:

I get error:

in.

But if I load and run it in the Windows Powershell ISE, it will give me the error the first time, and then act like it has “registered” the function and work after that.

And in case it is a procedural issue, I have tried listing the function at the top with no better luck.

Note I have tried simple functions like:

With the same exact results/error, it complains that Hello is not function….

Also, it still won’t every work just running the script in powershell (only in ISE after the first initial attempt).

Answer:

You need to declare your Select-Folder function before you try to use it. The script is read from top to bottom, so on the first pass when you try to use Select-Folder it has no idea what that means.

When you load it into the Powershell ISE it’ll find out what Select-Folder means on the first run, and it’ll still know that the 2nd time you try to run it (so you won’t get the error then).

So if you change your code to:

that should work each time you run it.

Source:

Why functions are not available locally until after first ran? by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply