I’ve just finished the MVA modules for “Getting Started with Microsoft PowerShell” (PS 3.0 JumpStart) with Helmick and Snover. In order to keep a ps1 in the active call stack, Snover says to “dot” it, a trick he calls, “dot-sourcing,” which keeps the script from being discarded from the call stack after execution; however, there is no mention of how to remove it from the stack afterward.
How can I remove a given script call from the working stack without starting a new session?
Why do you want to remove it? Or why don’t you like to start a new session?
It can be done in two ways…
Hard one. Know explicitly and enumerate everything which was dot-sourced (functions, variables…) and remove them explicitly.
Remove-Item Function:TheFunctionILoaded ...
Easy and recommended one. Create a PowerShell Module (PSM1) with your code (just rename the PS1 to PSM1). Use with
When no longer needed,
This is what modules are for (amongst many other nice functionalities).
BTW, dot-sourcing is not a trick, is a needed command to specify “Load a file contents, and keep it in memory”, in comparison to ./file.ps1 which means “Load the file, run it, forget it after running” (the usual script flow).
Olaf - Mostly curious. If there’s a way to turn something on, then there is usually a way to turn it off in the same session. Otherwise it could become a vulnerability.
JC - THANKS!