A `cd` command in an executed Powershell script still affects the current session

310 Views Asked by At

Coming from the Unix world, scripts when executed (as opposed to sourced) are run in a subshell, and thus variable declarations and cd commands do not affect the actual current session (the one where we type our commands).

For reference:

# Executing a script
./myscript.ps1

# Sourcing a script
. ./myscript.ps1

Very surprised (and quite frankly annoyed) to see than in Powershell, executing a script will still affect my current session regarding cd commands (but not declared variables).

How can I prevent this from happening ?

0

There are 0 best solutions below