Reputation: 2883
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?
Upvotes: 5
Views: 13814
Reputation: 6874
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.
Upvotes: 5
Reputation: 2448
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.
Upvotes: 13