Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to changed directory using batch file in PowerShell.

Let's say the batch file named go.bat has the two lines of code. And I'm in q: directory.

   c:
   cd c:\abc\def

When I run go.bat, I'm still in q:

share|improve this question
    
What exactly is not working? Are you running a .bat file from PowerShell and expecting it to change the PWD of the PowerShell host? – Rynant Mar 7 '12 at 19:23
1  
When you run a .bat file, the batch file runs in a child cmd.exe process. It won't change the directory of the parent PowerShell process. – Rynant Mar 7 '12 at 22:03
    
A .ps1 file is a PowerShell script, not what is commonly known as a batch (or command file) which is interpreted by command.exe and has a .bat or .cmd file extension. – martineau Mar 7 '12 at 22:13

While I agree that what you're doing should work, you can always try the Powershell cmdlet "set-location". It basically works the same way:

set-location c:\abc\def
share|improve this answer
    
@set-location works but it doesn't work when I run the command in batch file. – prosseek Mar 7 '12 at 21:58

Have you checked that C:\abc\def exists?

On my machine starting from the E: drive

  • C:
  • cd C:\windows\system32

works perfectly fine

share|improve this answer

Try:

   cd /D c:\abc\def

Use the /D switch to change current drive in addition to changing current directory for a drive -- although I'm not sure why what you tried isn't working...

If the folder doesn't exist you'll get an error message:

The system cannot find the path specified.

You can view some help on the cd command by typing cd /?.

share|improve this answer

When you run a batch file, PowerShell creates a CMD.EXE process to run the file. If you change the working directory inside the CMD.EXE process, it won't affect the current working directory of PowerShell. This is by design.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .