1

Currently, I am able to call cmd from powershell using the following command:

Start-Process cmd -ArgumentList '/K "ping 192.168.1.1"'

What I am trying to do is add multiple arguments to pass onto the command, but it is not working. For example:

Start-Process cmd -ArgumentList '/K "title test" /K "ping 192.168.1.1"' 

Is there a way to do this?

Edit: My goal is to have a cmd window open, pinging the address listed, but also to pass the "title" argument so the window is titled.

2 Answers 2

2

Since you're calling cmd.exe, use its statement-sequencing operator, &, to pass multiple commands to cmd /K:

Start-Process cmd -ArgumentList '/K title test & ping 192.168.1.1'

Note:

  • Start-Process's -ArgumentList (-Args) parameter technically accepts an array of arguments. While passing pass-through arguments individually may be conceptually preferable, a long-standing bug unfortunately makes it better to encode all arguments in a single string - see this answer.

  • cmd.exe's built-in title command inexplicably includes double quotes in the title if you enclose the argument in "..."; thus, if you want to specify a title that contains spaces, leave it unquoted and escape metacharacters other than spaces with ^; e.g., to pass test & more as the title, use title test ^& more

Sign up to request clarification or add additional context in comments.

Comments

1

start-process is sometimes tricky... try to add the elements as strings to an array and pass it over to start-process.

But in your case... idk what that /K should do, but in case of ping - ping is the process to start, not cmd ;-)

start-process ping.exe -argumentlist "127.0.0.1"

start-process ping.exe -argumentlist "127.0.0.1 /t"

as you are already using PowerShell

test-connection 127.0.0.1

Here is an example where I did something simliar:

$cmdArray = @(
                If ($token){
                    "-c"
                    "`"http.extraHeader=Authorization: Bearer $token`""
                }
                If ($clone){
                    'clone'
                }
                If ($fetch){
                    'fetch'
                    '-f'
                    'origin'
                    If ($tag){
                        "tags/$($tag):tags/$($tag)"
                    }
                }
                Else {
                    "`"$uri`""
                }
                If ($whatif){
                    '--dry-run'
                }
            )
            $result = Start-Process $pathGitexe -ArgumentList $cmdArray -Wait -NoNewWindow -PassThru

ok, based on your comment you need this:

$ips = @("192.168.1.1","192.168.1.2")

$ips | %{
    start-process ping.exe -ArgumentList $_
}

3 Comments

I wanted to use powershell to open cmd so i can only multiple ping windows all at once so i can track the ping of a few things at the same time.
Your edit does work, but I need to be able to rename the title of the cmd window by using another command. Not sure how to accomplish that.
cmd /K <command-line> allows you to open an interactivecmd.exe session that executes <command-line> on startup (/C, by contrast, exits the session after executing the command line). Thus, since the goal is to pass multiple statements as part of <command-line>, they can simply be separated with &.

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.