Share via


How to Run batch file every 5 seconds thru windows powershell script

Question

Wednesday, January 29, 2020 10:39 AM

Hi,

     I have one batch file. I wants to run that .bat file every 5 seconds. In that batch i have handled to open one .exe file to check the folder every 5 seconds files is there or not. so i have created one C# windows service to run every 5 seconds.it's working, but it's consume CPU usage high. so that i have to run that same .bat file through Powershell script. i have no idea about that. Please can someone help to run .bat file through Powershell script pls share some working example.

Thanks 

Manikandan.

All replies (2)

Wednesday, January 29, 2020 12:18 PM

HI Manikandan,

this is pretty simple. Please follow this one:

How to Execute a .bat File within a PowerShell Job

Regards,

(Please take a moment to "Vote as Helpful" and/or "Mark as Answer" where applicable. This helps the community, keeps the forums tidy, and recognizes useful contributions. Thanks!) Blog: https://blog.pohn.ch/ Twitter: @StoyanChalakov


Wednesday, January 29, 2020 3:12 PM

     I have one batch file. I wants to run that .bat file every 5 seconds. In that batch i have handled to open one .exe file to check the folder every 5 seconds files is there or not. so i have created one C# windows service to run every 5 seconds.it's working, but it's consume CPU usage high. so that i have to run that same .bat file through Powershell script. i have no idea about that. Please can someone help to run .bat file through Powershell script pls share some working example.

There is no need to use Powershell to run a bat file. Convert your bat file to a PS1 file and use the Powershell version of the commands. 

Having said that, you already have the correct solution, a true Windows service. Implement a file system watcher and process the files as they arrive. That eliminates a lot of the overhead of running something every five seconds.

Just do an internet search for examples, like this. https://www.infoworld.com/article/3185447/how-to-work-with-filesystemwatcher-in-c.html