Dot-sourcing a script without executing it


Is it possible in Powershell to dot-source or re-use somehow script functions without it being executed? I’m trying to reuse the functions of a script, without executing the script itself. I could factor out the functions into a functions only file but I’m trying to avoid doing that.

Example dot-sourced file:

Example consuming file:

Execution results:

Desired result:


You have to execute the function definitions to make them available. There is no way around it.

You could try throwing the PowerShell parser at the file and only executing function definitions and nothing else, but I guess the far easier approach would be to structure your reusable portions as modules or simply as scripts that don’t do anything besides declaring functions.

For the record, a rough test script that would do exactly that:

You will have problems if functions declared in the script reference script parameters (as the parameter block of the script isn’t included). And there are probably a whole host of other problems that can occur that I cannot think of right now. My best advice is still to distinguish between reusable library scripts and scripts intended to be invoked.


Dot-sourcing a script without executing it by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply