0

I am stuck in this weird problem where I am trying to execute a powershell script from the powershell command prompt. But neither do I get any errors nor the script is loaded.

I have script in C:\temp\myFunction.ps1 (which has a method getMyName() )

I open the powershell command and navigate to this directory and execute

./myFunction.ps1

then there are no errors and return back to the next line in the prompt. But when I try to call the function getMyName - I get error getMyName is not recognised.

I have set the Execution-Policy to Unrestricted, I am running the powershell as Administrator

1
  • It would be interesting to see what you mean exactly with: the function has a method. Commented Jun 15, 2012 at 10:43

2 Answers 2

5

Try dot sourcing your script:

. .\myFunction.ps1 
Sign up to request clarification or add additional context in comments.

2 Comments

Yes It works but I fail to understand the concept. I am really thankful to you
Your getMyName() is a function defined in your myFunction.ps1. It lives in the scope of myFunction.ps1. To have it in the console's scope you need to dot source the myFunction.ps1 file. Read technet link posted in my answer.
1

It's basic problem of Powershell script. Set the Path where you physically saved your file and then execute the Powershell script. One more thing

1. start your command window run as admin.
2. set the Powershell script policy for execution.

3 Comments

Will try by setting the path .. the . ./abc.ps1 solution also works but this i need to see. thanks
@ajax: Most welcome.. you will study this link. I face same problem and I resolve it too. stackoverflow.com/questions/9033847/…
Hi Guys I did some extra search and found out that declaring the functions as global will also solve this issue. function Example - global:myFunction()

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.