Welcome Forums General PowerShell Q&A "dot-sourcing"

This topic contains 3 replies, has 3 voices, and was last updated by

2 years, 2 months ago.

  • Author
  • #59292

    Points: 24
    Rank: Member

    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?

    Patrick (Krieger-bot)

  • #59295

    Points: 642
    Helping Hand
    Rank: Major Contributor

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

  • #59298

    Points: 15
    Rank: Member

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


  • #59571

    Points: 24
    Rank: Member

    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!

The topic ‘"dot-sourcing"’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort