How to create a function name to the global scope in a Powershell script file
I created a script named s.ps1 to define a function named s1.
# s.ps1
function s1
{
//
}
But, after I run this script, the s1 function is not available in the global scope like:
.\S.ps1
S1
s1' is not recognized as a cmdlet, function, operable program, or script file.
There are two ways to add the function name to the global scope.
1) Using the dot sourcing
. .\s.ps1 # be cautious that there are a space between two dots.
This makes all definitions in the script be exported or copied to the global scope after its execution.
2) Using global: keyword as follows:
# s.ps1
function global:s1
{
//
}
Comments
- Anonymous
July 18, 2006
PingBack from http://microsoft.wagalulu.com/2006/07/18/how-to-create-a-function-name-to-the-global-scope-in-a-powershell-script-file/ - Anonymous
July 18, 2006
You forgot to actually use the global: keyword in your 2nd example. - Anonymous
July 18, 2006
Thanks I fixed it.