How to write a Batch File on Windows

Hello Experts

Can someone show me how to compile a batch file that will run each time Windows  boots up?

I would like to run the following in a batch file

robocopy z:\mytestfolder c:\myhardfolder /move /minage:1

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

JohnBusiness Consultant (Owner)Commented:
Make a file with the command above in it. Call it firstbatch.bat or such like.

Now open Task Scheduler and make a task to run this batch file at start up. You need to use administrative credentials to run the file.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Hello ThereSystem AdministratorCommented:
It's easy. If you are in a domain, assign your script to the user as logon script. If not, use Task Scheduler to do it.

Edited: John H., you were 30 sec faster. :D
Hello ThereSystem AdministratorCommented:
E.g. create a new task that will run Powershell.exe under admin account and then in Arguments field write the path to your script.
(arguments:  -ExecutionPolicy RemoteSigned -File C:\script.bat)
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Nope, Hello There, you do not use PowerShell to run .bat or .cmd files ... It may work (e.g. for the single line used here), but if it is overkill.
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
You do not need a batch file. Just run the command in cmd, that is use the line
cmd /c robocopy z:\mytestfolder c:\myhardfolder /move /minage:1

Open in new window

in Task Scheduler.
JohnBusiness Consultant (Owner)Commented:
However you do it, Task Scheduler is a nice way to set it. You may find a batch file (even though only one line) is easy for you to follow.
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
A batch file is easier to change for most people. And if the command is more complex, I would not try to go without batch file.
However, I just noticed that we do not need to run cmd. RoboCopy is a self-contained executable, able to run on its own, as long as we do not use file redirection or the like. So it should be sufficient to run
robocopy z:\mytestfolder c:\myhardfolder /move /minage:1

Open in new window

as a task.
Joe Winograd, Fellow&MVEDeveloperCommented:
There have been numerous suggestions about creating a task in Task Scheduler. If you're not familiar with how to do that, this Experts Exchange 5-minute video Micro Tutorial will get you going on it:
How to use the Windows Task Scheduler - An Introduction

Btw, an alternative to using the Task Scheduler is to put a shortcut to the batch file in the Startup program group — it will then run when Windows starts. Regards, Joe
Joe Winograd, Fellow&MVEDeveloperCommented:
The question is how to execute a robocopy command each time Windows starts. Every post in this thread is helpful in answering that question. There is no "Best" answer, so I selected the first one posted as the Accepted Solution and all the others as Assisted Solutions, with the points split evenly among the experts.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows OS

From novice to tech pro — start learning today.