When running a powershell script which changes a directory via cd (or set-location/push-location/etc.), the console that the script was run from also ends up in that directory.
Example: script.ps1
cd c:\tmp
# do stuff
If I run this from c:\users\me, i end up in c:\tmp.
PS C:\users\me> .\script.ps1
PS C:\tmp> |
Now I know that I could use push-location and later do pop-location. However this wouldn't work if the script stopped somewhere in the middle (via Exit).
How can I solve this? Why doesn't the script have it's own location stack?
Try/Catch/Finally. Set the current directory path to a variable and thencd c:\tmpbefore theTry, and have the directory changed to variable in theFinally?powershell -File .\script.ps1exit.