4

I had batch script for running NPM which when simplified looks like this:

cd ./subfolder
call npm run dev

Then after launching it inside Powershell, NPM server was running. When I press Ctrl+C twice to close it, my current directory is left as it was, which is fine. However, later I needed more sophisticated tasks in the script and I changed it to Powershell script, which when simplified looks like this:

Set-Location ./subfolder
npm run dev

But now when I run it from Powershell and close, my working directory is left at "subfolder", which is really annoying!

Does anybody know how to solve it, so Powershell script behaves the same as batch file?

EDIT: I noticed, that opposite is true, if I call batch script from Command Prompt, then current directory is also kept at "subfolder".

EDIT2: I tried putting Set-Location .. in the end. The problem is that when I press Ctrl+C to close NPM (it is never-ending process), not only NPM is closed, but also powershell script, therefore last command is never reached.

2
  • I'm not a powershell user but an approach is to save the directoy in an environment variable and the do a cd back to the saved variable. You could do a cd - but that only works if there were no intervening changes.
    – Hogstrom
    Commented Dec 15, 2021 at 18:08
  • @Hogstrom: cd - is a feature of some Unix shells; it does not work (ever) in PowerShell. Or in COMMAND.EXE and 'batch' files, for that matter. Commented Oct 26, 2023 at 1:00

3 Answers 3

4

As a improvement of the suggestion by DavidPostill, you may use the PowerShell TRY/FINALLY commands to gracefully terminate the script:

try
{
    pushd ./subfolder
    npm run dev
}
finally
{
    write-host "Ended work."
    popd
}
3
  • With endless loop, NPM is restarted. If I remove loop, then it actually works! Thanks.
    – Somnium
    Commented Dec 16, 2021 at 14:36
  • 1
    Sorry, the endless loop was part of my testing. I took it out now.
    – harrymc
    Commented Dec 16, 2021 at 14:37
  • My problem was I didn't wrap the entire script with a try and remove the internal tries. Commented Oct 25, 2023 at 14:28
1

How do I restore the previous directory in a Powershell script?

Use pushd / Push-Location and popd / Pop-Location:

pushd ./subfolder
npm run dev
popd

Further Reading

Source

3
  • 1
    This doesn't work. I tried before similar solution with "cd .." (your's obviously cleaner). The problem is that when I press Ctrl+C to close NPM (it is never-ending process), not only NPM is closed, but also powershell script, last command is never reached.
    – Somnium
    Commented Dec 15, 2021 at 18:11
  • @Somnium If you force terminate a script then all bets are off.
    – DavidPostill
    Commented Dec 15, 2021 at 18:16
  • But when I use Batch script from Powershell, then directory is not changed, so it is possible, at least by making a wrapper script, but that is ugly. I also could a third scripting language...
    – Somnium
    Commented Dec 15, 2021 at 18:26
0

Instead of running the script inside of a command/PowerShell prompt:
./script.ps1

You can run it in a new prompt:
pwsh ./script.ps1

This way, the script will not affect the prompt in which you are executing the command.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .