Add function in Powershell

Posted on 2015-02-06
Last Modified: 2015-02-06

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.
Question by:janhoedt
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
LVL 40

Expert Comment

ID: 40593326
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..

Author Comment

ID: 40593428
No, not clear at all.

Author Comment

ID: 40593460
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 ...
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.


Author Comment

ID: 40593471
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 .

Author Comment

ID: 40593473
So that is . .\Myscript.ps1
LVL 40

Accepted Solution

Subsun earned 500 total points
ID: 40593482
You either have to add the complete path
PS> . c:\powershell\Function.ps1

Open in new window

or if the script is in same path where you run the script then (as you mentioned in your post)
. .\Function.ps1

Open in new window

LVL 40

Expert Comment

ID: 40593517
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..

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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor ( Top Charts is a view in which you can set seve…

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question