0

I am learning Windows PowerShell and I am struggling with the very basic task, how to create a .bat file to change the current directory? The simple .bat file with cd mydir inside worked well using cmd.exe, but it does not work in PowerShell:

PS C:\Users\ET\test> dir


    Directory: C:\Users\ET\test


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----        01/10/2021     10:57                mydir
-a----        01/10/2021     10:58             10 changeDir.bat


PS C:\Users\ET\test> type changeDir.bat
cd mydir
PS C:\Users\ET\test> .\changeDir.bat

C:\Users\ET\test>cd mydir
PS C:\Users\ET\test>

You see that my current directory has not changed after executing the .bat file. Works as expected using cmd.exe:

C:\Users\ET\test>changeDir

C:\Users\ET\test>cd mydir

C:\Users\ET\test\mydir>
  • 6
    Why don't you use PowerShell scripts and commands when using Powershell instead of `cmd` commands and scripts? The Powershell command for changing the working folder is `Set-Location` – Stephan Oct 01 '21 at 10:43
  • Thank you, Set-Location works fine from the command line, but script execution is disabled by default, and I am not using elevated PowerShell. – Evgeniy Tishin Oct 01 '21 at 11:35
  • 2
    Evgeniy, you state that script execution is disabled, is computer a standalone or connected to a network where the Administrator has disabled script execution? If it is a standalone computer you can just use the Set-ExecutionPolicy RemoteSigned command from an admin level powershell console to fix that and it's a one time command. – RetiredGeek Oct 01 '21 at 13:32
  • 1
    Based on your [comment below](https://stackoverflow.com/questions/69404268/how-to-change-the-current-directory-using-bat-file-in-windows-powershell#comment122675625_69404463) I would advise you to use Powershell's ALIAS or FUNCTIONS capability. You could even do it by creating a DOSKEY Macro that is executed within Powershell. – Squashman Oct 01 '21 at 13:37
  • 1
    Would also like to point out that if you have a directory opened in Windows Explorer and want to open up PowerShell to that directory, all you have to do is type `powershell` in the path bar. This also works for cmd.exe – Squashman Oct 01 '21 at 13:55

3 Answers3

3

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
mklement0
  • 382,024
  • 64
  • 607
  • 775
0

try the following

$scriptpath = $MyInvocation.MyCommand.Path
$dir = Split-Path $scriptpath
Write-host "running in directory $dir"
chkdsk
  • 81
  • 1
  • 8
  • Frankly. I do not understand the code above, sorry. – Evgeniy Tishin Oct 01 '21 at 11:36
  • 1
    i thought you have a finished script and would like to work from the script directory – chkdsk Oct 01 '21 at 12:26
  • My task is much simpler. When I start PowerShell it always starts in my home directory. I need to quickly jump to my working directory and I bored typing every time cd command with quite a long path. – Evgeniy Tishin Oct 01 '21 at 13:14
  • As an aside: In PSv3+ `$PSScriptRoot` directly contains the directory in which the enclosing script is located. – mklement0 Oct 01 '21 at 16:12
0

Actually, I found a way to start PowerShell in the directory I need. For that, I am using Windows Terminal app https://www.microsoft.com/en-gb/p/windows-terminal/9n0dx20hk701. I've configured the settings.json file this way:

        "guid": "{574e775e-4f2a-5b96-ac1e-a2962a402336}",
        "name": "PowerShell",
        "source": "Windows.Terminal.PowershellCore",
        "startingDirectory": "C:\\Haskell\\HaskellWikiBook"