There are three options:
- Pass the commands into
cmd.exe
when you start it
- Send the commands to
cmd.exe
after it has started
- Writing a batch file or PowerShell script
Pass the commands into cmd.exe
when you start it
To pass commands to cmd.exe
you can use the -k
or -c
parameters of cmd.exe
.
For example, this will start cmd.exe
, display the logged on user and then terminate the cmd.exe
process:
cmd.exe /c "whoami"
Look at this page or Google for more examples.
Send the commands to cmd.exe
after it has started
The alternative is to first start cmd.exe
and later send commands to it. There are quite a few questions about that around already, but there generally seem two approaches:
- Send one key at a time, e.g. How can I send keypresses to a running process object?
- Send data to the input buffer of the process, e.g. .NET: Inject Data into Input-Buffer of Process
I'm not sure if they'll work with cmd.exe
.
Writing a batch file or PowerShell script
The more common approach to automating cmd.exe
is to write a batch file or PowerShell script. You may also want to consider these options.