?
Solved

Add function in Powershell

Posted on 2015-02-06
7
Medium Priority
?
99 Views
Last Modified: 2015-02-06
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.
0
Comment
Question by:janhoedt
  • 4
  • 3
7 Comments
 
LVL 40

Expert Comment

by:Subsun
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..
http://blogs.technet.com/b/heyscriptingguy/archive/2010/08/10/how-to-reuse-windows-powershell-functions-in-scripts.aspx
0
 

Author Comment

by:janhoedt
ID: 40593428
No, not clear at all.
0
 

Author Comment

by:janhoedt
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 ...
0
Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

 

Author Comment

by:janhoedt
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 .
0
 

Author Comment

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

Accepted Solution

by:
Subsun earned 2000 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

0
 
LVL 40

Expert Comment

by:Subsun
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..

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

Featured Post

A Cyber Security RX to Protect Your Organization

Join us on December 13th for a webinar to learn how medical providers can defend against malware with a cyber security "Rx" that supports a healthy technology adoption plan for every healthcare organization.

Question has a verified solution.

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

A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
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ā€¦
Screencast - Getting to Know the Pipeline

864 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