Because a batch file (.bat
, .cmd
) runs in a child process (via cmd.exe
), you fundamentally cannot change PowerShell's current directory with it.
- This applies to all calls that run in a child process, i.e. to all external-program calls and calls to scripts interpreted by a scripting engine other than PowerShell itself.
- While the child process' working directory is changed, this has no effect on the caller (parent process), and there is no built-in mechanism that would allow a given process to change its parent's working directory (which would be a treacherous feature).
The next best thing is to make your .bat
file echo (output) the path of the desired working directory and pass the result to PowerShell's Set-Location
cmdlet.
# Assuming that `.\changeDir.bat` now *echoes* the path of the desired dir.
Set-Location -LiteralPath (.\changeDir.bat)
A simplified example that simulates output from a batch file via a cmd /c
call:
Set-Location -LiteralPath (cmd /c 'echo %TEMP%')
If you're looking for a short convenience command that navigates to a given directory, do not use a batch file - use a PowerShell script or function instead; e.g.:
function myDir { Set-Location -LiteralPath C:\Users\ET\test\myDir }
Executing myDir
then navigates to the specified directory.
You can add this function to your $PROFILE
file, so as to automatically make it available in future sessions too.
You can open $PROFILE
in your text editor or add the function programmatically, as follows, which ensures on-demand creation of the file and its parent directory:
# Make sure the $PROFILE file exists.
If (-not (Test-Path $PROFILE)) { $null = New-Item -Force $PROFILE }
# Append the function definition to it.
@'
function myDir { Set-Location -LiteralPath C:\Users\ET\test\myDir }
'@ | Add-Content $PROFILE