Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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:

Solved

In Powershell, I needed to name the batch file extension as .ps1, with .bat, I got errors.

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

3 Answers 3

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.