Powershell functions not recognized when calling from command line
powershell, powershell-2.0
Solution
Use `Import-Module` cmdlet in powershell session(window):
Import-Module 'C:\My Path\script.ps1'
then you can run `MyFunction $var1 $var2` for the duration of that session.
If you want your script to execute a function contained within it, then add the following line to the bottom. It assumes you are passing two arguments in when executing the script( ie. `powershell 'C:\My Path\script.ps1' "value1" "value2"`)
MyFunction $args[0] $args[1]
Problem
I have a simple problem. My powershell script contains a couple of functions and the script calls these functions no problem. However, when I try to run powershell from the command line to execute my script, I get the error; ``` The term 'MyFunction' is not recognized as the name of a cmdlet....' ``` Everywhere tells me I need to run my script like so from the command line for it to work; ``` powershell "& "'C:\My Path\script.ps1'" ``` This runs the script but the functions still do not work. I get the errors wherever my script calls one of the functions. What am I doing wrong? I have even gone as far as creating another script with a single line in it that just says; ``` . C:\My Path\script.ps1 ``` This again works fine but then when run through the command line it fails with the same errors. Any help with this would be greatly appreciated! EDIT: The function is defined like this; ``` function MyFunction ($id1, $id2) { .... } ``` And it is called like this; ``` MyFunction $var1 $var2 ``` The following script file resolved my issue and can be run from the command line fine; ``` Import-Module 'C:\My Path\script.ps1' MyStartFunction # calls other functions powershell "& "'C:\My Path\AboveScriptFile.ps1'" ```