0

So I am trying to write a script that allows me to open several cmd command prompt and write in them the same command but with different variables.

The solution that I came with was to write a PowerShell script that calls inside a loop a cmd file and pass a variable each time to the cmd file but I'm stuck, the PowerShell script execute only one cmd.

Can someone help to figure this out ?

Thanks :)

1
  • 2
    Show us your code :) Commented Apr 19, 2022 at 12:23

3 Answers 3

2

You can use the following :

cmd.exe /c [command]

for example

$x = 1..100
foreach ($n in $x){cmd.exe /c ping 192.168.1.$n}
Sign up to request clarification or add additional context in comments.

1 Comment

thank you for your response but your solution does not allow me to open a cmd prompt when i need to open a cmd first than execute some commands for my request
1

mohamed saeed's answer shows you to execute cmd.exe commands synchronously, in sequence in the current console window.

If, by contrast, you want to open multiple interactive cmd.exe sessions, asynchronously, each in its separate, new window, use cmd.exe's /k option and invoke cmd.exe via Start-Process:

# Open three new interactive cmd.exe sessions in new windows 
# and execute a sample command  in each.
'date /t', 'ver', "echo $PSHOME" | ForEach-Object {
  # Parameters -FilePath and -ArgumentList implied.
  Start-Process cmd.exe /k, $_
}

Note:

  • Unless your cmd.exe console windows have the Let the system position the window checkbox in the Properties dialog checked by default, all new windows will fully overlap, so that you'll immediately only see the last one opened.

  • The last new window opened will have the keyboard focus.

  • The commands passed to /k are instantly executed, but an interactive session is then entered.

Comments

0

If you would like to keep in purely batch, you can use the start command. The /k switch keeps the command line open. You would use /c if you want to carry out the command and terminate :

start "" %comspec% /k ping 192.168.1.1

From powershell, you can use the Start-Process command with an ArgumentList:

Start-Process cmd.exe -ArgumentList "/k ping 192.168.1.1"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.