Search code examples
powershellwindows-terminal

Windows Terminal not changing directory via script


I have file assist.cmd, which is within my %path% with the contents

pushd m:\Documents\Code\assist

When I type assist on windows terminal (the new one) I get:

PS C:\Users\User> assist

C:\Users\User>pushd m:\Documents\Code\assist
PS C:\Users\User>

When I copy the pushd command myself, it works but not when used from the script.

Any ideas?


Solution

  • When you start a BAT script "assist.cmd" a new child process "cmd.exe" is started. The command processor executes the BAT script and the current working directory of that process is actually changed. Thereafter the process is terminated after the BAT script completed.

    The current working directory of a child process does not change the working directory of the PowerShell process. That's why a .cmd script is not usable to perform the intended assistance.

    You can use a PowerShell script "assist.ps1" that contains the Push-Location or its alias pushd and call it with the dot sourcing syntax

    . assist.ps1