desmond13
desmond13

Reputation: 3149

batch file to open two (or more) powershells in two (or more) specific folders

I am trying to do something really basic in a batch script, but it is not working.

I want to open two PowerShell windows each with a different current working directory.

I am using the following script

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noexit -command "cd c:\Users\User1" 
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noexit -command "cd c:\Users\User2"

Unfortunately it opens only the first window.

For sure it is only a syntax error but I could not find a solution yet.

Upvotes: 1

Views: 262

Answers (3)

Compo
Compo

Reputation: 38708

The Start command already has a 'working directory', option, /D.

@Start /D "C:\Users\User1" %SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoExit
@Start /D "C:\Users\User2" %SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoExit

You could of course do it with a simple For loop:

@For %%G In ("C:\Users\User1" "C:\Users\User2") Do @Start /D "%%~G" %SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoExit

Upvotes: 2

CodingFox
CodingFox

Reputation: 53

start C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noexit -command "cd c:\Users\User1"
start C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noexit -command "cd c:\Users\User2"

You just need to add "start" at the beginning

Upvotes: 1

desmond13
desmond13

Reputation: 3149

As it happens often: I posted the question after researching a lot and then I immediately found the answer!

This is what you should write in your batch file:

START C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noexit -command "cd c:\Users\User1" 
START C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noexit -command "cd c:\Users\User2"

I hope my answer will help someone in the same situation.

Upvotes: 3

Related Questions