Search code examples
windowspowershellstdinread-eval-print-loop

How can I keep powershell in repl mode with a loaded script


I'm trying to start a powershell instance, that loads a script and remains open so I can still call methods loaded by that script manually.

I'm trying to dot source a script and pipe it to powershell like below, from a cmd instance/batchfile:

echo . .\script.ps1 | powershell

The result in this case is that powershell starts, loads my script, executes it and exits. I've tried running with -noexit argument, it has no effect.

I'm thinking of another option, to start a powershell process and pipe my dot source command to its stdin - but this probably won't allow me to interact with the process anymore because its stdin is opened by the host process.


Solution

  • If you need to run a script file so that window stays open and variables are accessible after the execution.

    Try dot sourcing the script file like this:

    powershell -noexit ". .\script.ps1"
    

    Once the script is done, you can access any internal variable the script defined. Assuming the variables are at the script level scope.