Module scope
Script modules loaded from psm1
files have a shared scope that you can access using the $Script:
scope modifier.
You can create variables in the module scope, which is the same as the script scope. Functions within the module may consume those variables. You can create such variables in the root module or when a command is run.
Helper functions can be created to provide obvious access to the variable content.
This approach is illustrated in the following example. This pattern can be used for modules that interact with services that require a connection or authentication. For example, a REST web service might require an explicit authentication step to acquire a time-limited token or key.
First, a function styled like the following Connect-Service
function establishes a script-scoped variable. The connection can capture an authentication token rather than representing a truly connected service (for instance, a connection to an SQL server). The implementation...