Ian's Software for Mere Mortals

Youngjune or Ian's blog about Connectivity

How to create a function name to the global scope in a Powershell script file

How to create a function name to the global scope in a Powershell script file

  • Comments 3

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
{
             //
}