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.

4
  • Possible duplicate of How to keep the shell window open after running a PowerShell script? Commented Mar 27, 2018 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. Commented Mar 27, 2018 at 10:58
  • that solution does not work, powershell remains indeed open but I cannot access anything from script.ps1 Commented Mar 27, 2018 at 11:20
  • 1
    Try dot sourcing it inside of the powershell command, like this powershell -noexit ". .\script.ps1" Commented Mar 27, 2018 at 11:26

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.