Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

How to use function in other scriptfiles?

Closed Thread
 
Thread Tools Display Modes
Old 09-13-2006   #1 (permalink)
Jens Schulze
Guest


 

How to use function in other scriptfiles?

Hello
is it possible to run a function or scriptblock in another scriptfile? for
example i have a script "myfunctions.ps1" with some functions and
scriptblocks which i often use. now i wish to run in another script a
defined function in "myfunction.ps1".

with best regards Jens


Old 09-13-2006   #2 (permalink)
=?Utf-8?B?Um9tYW4gS3V6bWlu?=
Guest


 

RE: How to use function in other scriptfiles?

You have to "dot source" your script myfunctions.ps1 into another script or
interactive session:

.. myfunctions.ps1

Now you may use functions from myfunctions.ps1.

--
Thanks,
Roman

Old 09-13-2006   #3 (permalink)
=?Utf-8?B?ZHJlZXNjaGtpbmQ=?=
Guest


 

RE: How to use function in other scriptfiles?

"Jens Schulze" wrote:

> Hello
> is it possible to run a function or scriptblock in another scriptfile? for
> example i have a script "myfunctions.ps1" with some functions and
> scriptblocks which i often use. now i wish to run in another script a
> defined function in "myfunction.ps1".
>
> with best regards Jens
>


Put all script files that contain the functions you need at the beginning of
your script and put a dot "." and a space infront of it. This is called
"dot"-sourcing and will run that script in the current scope (i.e. the scope
of your current script file). Thus, all functions that you define in
myfunctions.ps1 will be defined after the beginning of your script file. Note
that these functions won't be defined anymore once your script has ended. You
can "load" your all functions that are defined script files at the
interactive prompt in the same way.

Example:

#################
# myfunction.ps1
#################
function do-foo {"foo"}
#################


#################
# myscript.ps1
#################
.. C:\mypath\myfunction.ps1

do-foo
#################

--
greetings
dreeschkind
Old 09-13-2006   #4 (permalink)
Jens Schulze
Guest


 

Re: How to use function in other scriptfiles?

Hello Roman

"Roman Kuzmin" <RomanKuzmin@discussions.microsoft.com> schrieb im
Newsbeitrag news:2F75C344-3D70-452A-A1C1-B81F59F94557@microsoft.com...
> You have to "dot source" your script myfunctions.ps1 into another script
> or
> interactive session:
>
> . myfunctions.ps1
>
> Now you may use functions from myfunctions.ps1.
>

As i understand your post I renamed the file "myfunctions.ps1" into
".myfunction.ps1" is this correct? The file ist saved in a normal folder
like G:\myscripts\. in this folder the calling script is saved, too.

If i want now to call the function getpfad() within the
..myfunctions.ps1-file, how can I call them? I dont find a way.
..myfunctions.ps1.getpfad()
don't works
G:\myscripts\.myfunctions.ps1.getpfad()
don't works, too

with best regards, Jens





Old 09-13-2006   #5 (permalink)
Jens Schulze
Guest


 

Re: How to use function in other scriptfiles?

thank you, now i understood. works fine.

Jens
"dreeschkind" <dreeschkind@discussions.microsoft.com> schrieb im Newsbeitrag
news:2ED361B5-7615-4EDC-8BE1-206DBBA58ED2@microsoft.com...
> "Jens Schulze" wrote:
>
>> Hello
>> is it possible to run a function or scriptblock in another scriptfile?
>> for
>> example i have a script "myfunctions.ps1" with some functions and
>> scriptblocks which i often use. now i wish to run in another script a
>> defined function in "myfunction.ps1".
>>
>> with best regards Jens
>>

>
> Put all script files that contain the functions you need at the beginning
> of
> your script and put a dot "." and a space infront of it. This is called
> "dot"-sourcing and will run that script in the current scope (i.e. the
> scope
> of your current script file). Thus, all functions that you define in
> myfunctions.ps1 will be defined after the beginning of your script file.
> Note
> that these functions won't be defined anymore once your script has ended.
> You
> can "load" your all functions that are defined script files at the
> interactive prompt in the same way.
>
> Example:
>
> #################
> # myfunction.ps1
> #################
> function do-foo {"foo"}
> #################
>
>
> #################
> # myscript.ps1
> #################
> . C:\mypath\myfunction.ps1
>
> do-foo
> #################
>
> --
> greetings
> dreeschkind



Closed Thread

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can I get two variables out of a function? ssg31415926 PowerShell 6 04-20-2008 07:29 AM
How can I tell if a function is already defined? Kevin Buchan PowerShell 3 02-06-2008 07:50 AM
Function - XML NeilOz PowerShell 4 01-10-2008 02:51 AM
possible function bug? klumsy@xtra.co.nz PowerShell 1 09-29-2006 06:02 PM
BUG: Redirecting function contents to a file truncates function lines at the width of the console Adam Milazzo PowerShell 2 08-11-2006 10:58 PM








Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50