0

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.

ktx
  • 168
  • 11
  • Possible duplicate of [How to keep the shell window open after running a PowerShell script?](https://stackoverflow.com/questions/16739322/how-to-keep-the-shell-window-open-after-running-a-powershell-script) – kim Mar 27 '18 at 10:57
  • In short, rearrange your command to read `powershell -noexit .\script.ps1` and it should run your script while keeping the window open once it's done. – kim Mar 27 '18 at 10:58
  • that solution does not work, powershell remains indeed open but I cannot access anything from `script.ps1` – ktx Mar 27 '18 at 11:20
  • 1
    Try dot sourcing it inside of the powershell command, like this `powershell -noexit ". .\script.ps1"` – kim Mar 27 '18 at 11:26

1 Answers1

1

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.

kim
  • 3,385
  • 2
  • 15
  • 21