Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - How to - named function in script file

Reply
 
Old 08-18-2006   #1 (permalink)
Thomy Kay


 
 

How to - named function in script file

Ok, I had now 3 days playing with PS. Played with all the cmdlets, piped
here and there.

One thing I can't accomplish is to put even the simplest function into a
script file, load and finally use it.

What I've done:

* edited in an editor
>

function test-me
{
"outtestme"
}
>


* saved as test-me.ps1
* in PS : cd <path>
* in PS : ./test-me

Got a prompt without error (expected to have the function defined now)

* in PS : test-me

get

'Test-me' is not recognized as a cmdlet, function, operable program, or
script
file.
At line:1 char:7
+ Test-me <<<<

Where is the trick?

Thanks, Thomas

My System SpecsSystem Spec
Old 08-18-2006   #2 (permalink)
=?Utf-8?B?U3VuZyBNIEtpbQ==?=


 
 

RE: How to - named function in script file

"Thomy Kay" wrote:

You would have to dot-source the file into the currently executing
powershell session

[^_^]PS[110]+>. .\test-me.ps1
[^_^]PS[111]+>test-me
outtestme

> Where is the trick?


The trick is the dot, "."

--
Sung M Kim

Please don''t bother me with spam...
My System SpecsSystem Spec
Old 08-19-2006   #3 (permalink)
Bruce Payette [MSFT]


 
 

Re: How to - named function in script file

The other thing you can do is to define a global function in the script
like:

function global:foo { "Hi there" }

-bruce

--
Bruce Payette [MSFT]
Windows PowerShell Technical Lead
Microsoft Corporation
This posting is provided "AS IS" with no warranties, and confers no rights.
Visit the Windows PowerShell Team blog at:
http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at:
http://www.microsoft.com/technet/scr.../hubs/msh.mspx


"Thomy Kay" <thomy.kay@online.de> wrote in message
news:uBBHRE4wGHA.560@TK2MSFTNGP05.phx.gbl...
> Soo easy! Anyway, you have to know it. Thanks!
>
> Thomy
>
> Sung M Kim wrote:
>> "Thomy Kay" wrote:
>>
>> You would have to dot-source the file into the currently executing
>> powershell session
>>
>> [^_^]PS[110]+>. .\test-me.ps1
>> [^_^]PS[111]+>test-me
>> outtestme
>>
>>> Where is the trick?

>>
>> The trick is the dot, "."
>>



My System SpecsSystem Spec
Old 08-19-2006   #4 (permalink)
Thomy Kay


 
 

Re: How to - named function in script file

Soo easy! Anyway, you have to know it. Thanks!

Thomy

Sung M Kim wrote:
> "Thomy Kay" wrote:
>
> You would have to dot-source the file into the currently executing
> powershell session
>
> [^_^]PS[110]+>. .\test-me.ps1
> [^_^]PS[111]+>test-me
> outtestme
>
>> Where is the trick?

>
> The trick is the dot, "."
>

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Where can I put function in script? PowerShell
'Write-object' is not recognized as a cmdlet, function, operable program, or script file PowerShell
Script-Function Help PowerShell
BUG: Redirecting function contents to a file truncates function lines at the width of the console PowerShell
Collecting named arguments in a script? PowerShell


Vista Forums 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 Ltd

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