"dot-sourcing"

This topic contains 3 replies, has 3 voices, and was last updated by  Patrick Chambers 10 months, 2 weeks ago.

  • Author
    Posts
  • #59292

    Patrick Chambers
    Participant

    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?

    Thanks,
    Patrick (Krieger-bot)

  • #59295

    Olaf Soyk
    Participant

    Why do you want to remove it? Or why don't you like to start a new session?

  • #59298

    JC Ruiz
    Participant

    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

     Import-Module ./yourmodule.psm1
    

    When no longer needed,

       Remove-Module YourModuleName
    

    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).

    http://www.powershelltraining.eu

  • #59571

    Patrick Chambers
    Participant

    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!

You must be logged in to reply to this topic.