4

I have a batch script on my computer called cs.bat. When I enter cs in the command prompt, pushd takes me to a certain directory and leaves me there. In PowerShell, the command does the same thing but then brings me back into the starting directory.

Why is this the case? How can I make it so that I stay in the directory after typing 'cs' into power shell?

Windows PowerhShell versus Windows command prompt and batch file edited in Notepad

Community
  • 1
  • 1
Sean Letendre
  • 2,623
  • 3
  • 14
  • 32

2 Answers2

11

Powershell includes aliases for Pushd and Popd.

Get-Alias Pushd : pushd -> Push-Location
Get-Alias Popd : popd -> Pop-Location

You can then use Get-Help Push-Location -Full -Online to get the latest help for that cmdlet.

Then just make a script and test this behavior.

#Sample.ps1 script

#Get current DIR
dir

#push location to some location and DIR there.
Push-Location C:\Scripts\
dir

#at this point, your console will still be in the Push-Location directory
#simply run the Pop-Location cmdlet to switch back.
user4317867
  • 2,397
  • 4
  • 31
  • 57
5

This is happening because your "cs.bat" runs in a different process (running cmd.exe) spawned by PowerShell (whereas batch files execute in the same instance when run from cmd). Current directory is a per-process concept, so changing it in one process has no effect on another.

Probably the simplest way to get around it is to write a "cs.ps1" script (or function), that would run in the PowerShell process.

Joey
  • 344,408
  • 85
  • 689
  • 683
Burt_Harris
  • 6,415
  • 2
  • 29
  • 64