Executing and calling functions
As seen in figure in the Comparing scripts and functions section, defining a function involves using the function
keyword and surrounding the code you want with a scriptblock. Executing this function statement simply adds the function definition to the current PowerShell session. To subsequently call the function, you use the function name like you would a cmdlet name, and supply parameters and arguments just like you would for a cmdlet or script.
Tip
Storing the MdAndGo
function in a file called MdAndGo.ps1
can be confusing because we're using the same name for the two things. You dot-source the ps1
file to load the function into the session, then you can execute this function. Dot-sourcing the file doesn't run the function. If we had written this as a script, on the other hand, we could have executed the logic of the script without dot-sourcing.
Tip
Warning!
A common mistake of new PowerShell users is to call a function using parentheses to surround the parameters...