Link to home
Start Free TrialLog in
Avatar of janhoedt
janhoedt

asked on

Add function in Powershell

Hi,

I'm new to Powershell, I do know my way around but the basics only.
I found a function which I like to implement.

How do I do this (where do I copy this function and how do I call for it)?

Please advise.
J.
Avatar of SubSun
SubSun
Flag of India image

You can dot source the script.. For example save the function it in to a ps1 file and run it in PowerShell console as shown below. Once it is done. Function saved in the .ps1 file will be available in console..


PS> . c:\powershell\Function.ps1

Open in new window

PS : There is a space after dot.

You can refer the following article for other examples..
http://blogs.technet.com/b/heyscriptingguy/archive/2010/08/10/how-to-reuse-windows-powershell-functions-in-scripts.aspx
Avatar of janhoedt
janhoedt

ASKER

No, not clear at all.
I have added the function but then what? If I call for the function I get errors "the term -myscript.ps1" is not recognized as the name of a cmdlet, function, script file ...
I had to add an extra . before the ps1.
My path is that long so I did not use c:\... just the .\myscript.ps1 that doesnt work unless you add an extra .
So that is . .\Myscript.ps1
ASKER CERTIFIED SOLUTION
Avatar of SubSun
SubSun
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
For example.. if you have following function saved in to a function.ps1 file in C:\temp

Function Test-Echo ($String){
Write-Host "Hello $String"
}

Open in new window


Here is how you use it..

User generated image
Another option is to just copy and paste the code in to the PowerShell console and then start using the function.
User generated image