Solved

Add function in Powershell

Posted on 2015-02-06
7
91 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
[X]
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
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
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

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 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

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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Set OWA language and time zone in Exchange for individuals, all users or per database.
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
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…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

730 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