Search code examples
powershellcmd

How to start cmd from within powershell and pass a command to the cmd instance?


I am trying to create an instance of a windows cmd terminal from powershell every minute for a maximum of 8 times and get each cmd instance to run a nodejs script from there.

Here is my code so far:

For ($i=0; $i -le 8; $i++) {
    start cmd.exe /k node index.js
    Start-Sleep -Seconds 60
    }

but I keep on getting errors:

Start-Process : A positional parameter cannot be found that accepts argument 'node'.
At C:\Users\user\Documents\x\x\build\src\start.ps1:2 char:5
+     start cmd.exe /k node index.js
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Start-Process], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.StartProcessCommand

I have already looked at this answer posted on SuperUser, however, it is not clear to me what I am doing wrong.

The second answer on this stack overflow thread seems to be doing exactly what I am trying to do, but I keep getting the above error.


Solution

  • Start is an alias for the Start-Process cmdlet as mentioned by @lit

    Any arguments have to passed on with the -ArgumentList parameter.

    start "cmd.exe"  -ArgumentList "/k node index.js"