SoFunction
Updated on 2025-03-10

Introduction to functions, scripts, and scopes of PowerShell Introduction

The script reflects the programming characteristics of PowerShell and is the basis of task automation. Functions are code multiplexing units that are more granular than scripts, and can be defined in the command line or in the script. Scope is the scope of function of variables and functions, and it is the division of execution context.

function

Functions are named command lists, which have the same category as the function concepts in general programming languages. There can not only be simple commands in the function, but also commands that control the flow, such as if, while, switch, etc. Functions can have anonymous parameters or named parameter lists. The command parameter list can be defined in curly braces or Param keywords. Anonymous functions can be accessed using the $Args variable. Functions can also receive objects from the pipeline as input, and the pipeline objects can be accessed through the $input variable class.

Functions defined in scripts can be defined anywhere after the #require command and Param keyword, but before the call is called. Also, custom functions will not run automatically and need to be called explicitly. Functions can be defined using filter or function. Functions defined using filter keywords are simpler, while functions defined using function keywords can have more complex functions.

Examples of simple function definitions are as follows:

Copy the codeThe code is as follows:

function SayHello
{
   "Hello"
}

The function call method is similar to using the Cmdlet method, enter SayHello and press Enter. Then the result is Hello.

script

A script is to store some commands into a file and set the extension of the text file to .ps1. In addition to using commonly used cmdlets and commands to control the flow in scripts, custom functions can also be defined and called. Calling methods are similar to calling cmdlet methods.

The script can also have parameters, either a named parameter or anonymous parameter. In terms of parameters, scripts are very similar to functions.

In addition, PowerShell's script execution policy does not allow execution of any script files by default. You can execute the following commands when modifying the execution policy: Set-ExecutionPolicy RemoteSigned. It should be remembered that modifying the execution strategy will bring security risks. Please think twice before modifying the execution strategy.

The example of simple script d:\greet.ps1 is as follows

Copy the codeThe code is as follows:

Param([String] somebody)
 
function Greet([String] name)
{
    "Hello $name"
}
 
echo "Call function Greet ..."
Greet $somebody

The script call method is as follows:
Copy the codeThe code is as follows:

d:\greet.ps1 "Luke"

or
Copy the codeThe code is as follows:

.\greet.ps1 "Luke"

The execution result of the above script is "Hello Luke".

Scope

By type, there are two scopes: global (global scope) and script (script scope). After starting the PowerShell command line, all command line commands run in the global scope. The script context runs in the script scope, and the variables and functions defined in the script are not visible after the run is completed. This is because variables and functions defined in scripts are in script scope by default. Of course, you can also display the scope of defining variables and functions, such as function global:fun1 (){…}. In this way, after the script is executed, fun1 can still be executed in the global scope.

According to the axis relationship, there can be parent scope, local scope (current scope), and child scope. These are not new scope types, but relative relationships between scopes. The subscope can also have subscopes, and this level can be very deep.

In addition to defining variables and functions in scripts in the global scope, you can also use the point "." to get the source and execute a normal script in the local scope. After executing and exiting the script, all variables and functions defined in the script will continue to be available in the local scope.

Use point "." to get the source example as follows:

Copy the codeThe code is as follows:

. d:\greet.ps1 "Luke"

or
Copy the codeThe code is as follows:

. .\greet.ps1 "Luke"

That is, point ".", space, and then the general script execution method.

Conclusion

Functions, scripts and scopes, each concept needs to be explained in detail and requires a lot of time. Here we will simply explain their concept, their relationships and simple usage. Give readers a rough impression and use it if possible.