Solved

Add function in Powershell

Posted on 2015-02-06
7
85 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

758 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now