Batch Script for opening the DOS prompt and run a command

Posted on 2016-08-29
Last Modified: 2016-09-18
Hi there,
    I need a batch script code which does the following
1)Open a command prompt
2)Run the Tasklist (Dos Command which provides the services and processes details and get the output in csv file)
    For this i run it in cmd prompt like as "tasklist>TasklistDetails.csv"
3)The above command from dos prompt works like charm, but however I am not sure how to put this command in .bat file and make to run as  windows  scheduled task

Question by:Deepak Kumar
  • 3
  • 2
  • 2
  • +1
LVL 35

Expert Comment

by:[ fanpages ]
ID: 41774570
If I understand your question correctly, I think you just need a simple ASCII Text file, say, "tasklist-cmd.bat", with the single line:


Use this file within the MS-Windows Task Scheduler (as attached, after renaming from "tasklist-cmd.bat.txt" to "tasklist-cmd.bat" in your local filing system).

The ".csv" file output will be created in the same folder where the ".bat" file is located...

...but this is not a Comma-Separated Values file (if that is what you were intending).

For that, the command will be:

tasklist /FO CSV>TasklistDetails.csv

(Please see attachment "tasklist-csv-cmd.bat.txt"; also rename to a file with just a ".bat" file extension).
LVL 12

Expert Comment

by:Gary Dewrell
ID: 41774646
fanpages has your answer, I just wanted to share an additional option.
If the OS you are running has powershell installed then this command will also give you want you want and allow you some more customizations such as what information you want.

get-scheduledtask | export-csv  tasklist.csv
LVL 54

Accepted Solution

Bill Prew earned 500 total points
ID: 41775667
You don't need a BAT script for this, just schedule the following command line to run in Task Scheduler.

cmd /c tasklist>c:\temp\TasklistDetails.csv

Open in new window

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.


Author Comment

by:Deepak Kumar
ID: 41775789
Hi All,

   Thank you all for your suggestions. I have setup a task in task scheduler based on Bill's suggestion.
Will post if anything required. You guys are really helping me out.

LVL 35

Expert Comment

by:[ fanpages ]
ID: 41796004
I've requested that this question be closed as follows:

Accepted answer: 250 points for [ fanpages ]'s comment #a41774570
Assisted answer: 125 points for gdewrell's comment #a41774646
Assisted answer: 125 points for Bill Prew's comment #a41775667

for the following reason:

Suggested split:

fanpages (Best Answer): 250 points
gdewrell (Assisted Solution): 125 points
Bill Prew (Assisted Solution): 125 points
LVL 54

Expert Comment

by:Bill Prew
ID: 41796005
Hard to say what the OP found as useful, but he certainly called out the last post by billprew as part of their solution, so I would suggest an equal split rather than a weighted split:

    [ fanpages ]'s comment #a41774570 (167 points)
    gdewrell's comment #a41774646 (166 points)
    Bill Prew's comment #a41775667 (166 points)

LVL 35

Expert Comment

by:[ fanpages ]
ID: 41796298
Answering the question posed is obviously not worth anything now.

This site gets worse by the day.


Author Comment

by:Deepak Kumar
ID: 41804319
Hi All,
  I have tried all of your suggestions and found that Bill's suggestion was easy to implement. Thank you all for your help.
According to me the points should be split as below,

1)FanPages -200 points

2)Bill Prew - 200 points

3) gdewrell's - 100 points

Please note, I feel that I have justified in giving points based on your assistance provided.


Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question