Search code examples
powershellsubshell

Execute Batch in Powershell (Win 10) does not affect Parent Shell


just for understanding this.

I want to open my Powershell in a certain folder. As I didn´t find out how, I tried to put a batch file with just "cd ....." in it in the default folder where PowerShell opens. When I execute the batch, though, I end up where I started from. It seems that the batch gets excuted in a subshell which doesn´t affect the Parentshell.

How can I execute the stuff in the batchfile in parentshell ?

Thanks in advance!


Solution

  • You cannot. Batch files are executed by cmd, not PowerShell, so there will always be a new process for them.

    With a PowerShell script you can use dot-sourcing

    . Script.ps1
    

    To execute the script in your current scope, which is most similar to how batch files are executed by cmd by default.