PowerShell 7 module lazy loading for low-traffic single script invocations
0 reputation · 10 May 2021, 20:20 UTC
Evaluating startup and execution cost for infrequent, single-script PowerShell invocations on Windows and cross-platform systems is a design concern for low-traffic workloads.
The goal is to minimize CPU, I/O and memory overhead per run. PowerShell 7 is documented to use module lazy loading, which differs from Windows PowerShell 5.1 behavior where referenced modules can be loaded earlier. Using non-interactive invocation is intended to avoid shell overhead, and local execution is generally preferred over remoting or background jobs for one-off runs.
An unresolved behavior is whether PowerShell 7's lazy loading can be relied upon for all third-party modules without triggering full loads. Some modules enforce initialization at parse time or via top-level side effects, which may defeat lazy loading assumptions.
Can module lazy loading be assumed safe for infrequent scripts that reference third-party modules? Which module patterns reliably avoid parse-time initialization in PowerShell 7? When does non-interactive script execution still incur module load cost?