How do I run a PHP script using windows schedule task?

How do I run a PHP script using windows schedule task?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Configuring Windows Task Scheduler for PHP Scripts

Running a PHP script on a local machine via the Windows Task Scheduler is a common requirement for cron-like functionality, but getting it right often hinges on how you define the execution environment. The core challenge isn't just launching php.exe; it's ensuring that the script runs with the correct working directory and access to necessary dependencies.

When you open the "Actions" tab in the Task Scheduler GUI, you will see a field for the program/script and another for its arguments. The standard approach is to point this to your PHP interpreter, typically located at C:\PHP\php.exe or wherever you installed it via XAMPP/WAMP/Laragon. However, simply typing php script.php is rarely sufficient for local development environments.

The first critical decision involves the Working Directory. By default, Windows Task Scheduler runs tasks under the context of your logged-in user, but it often executes from a generic system directory like C:\Windows\System32. If your PHP script relies on files located in your project's root folder (e.g., htdocs or a specific Laravel installation), and those paths are relative, the script will fail with a "file not found" error. To resolve this, you must explicitly set the working directory in the Task Scheduler properties under the General tab.

Additionally, you must ensure the environment variables match your development setup. If your script depends on .env files or specific configuration paths, the scheduler might not load them automatically if the current user profile settings don't mirror your interactive terminal session. A robust solution involves creating a dedicated batch file that sets these paths before invoking PHP. For example:

@echo off
cd /d "C:\path\to\your\project"
set PATH=C:\PHP;C:\path\to\vendor\bin;%PATH%
"C:\PHP\php.exe" artisan schedule:run

You can then configure the Task Scheduler to execute this batch file instead of calling php.exe directly. This approach guarantees that the command prompt starts in the correct directory and inherits the necessary environment variables.

Timing is another configurable aspect handled in the Triggers tab. Here, you define the recurrence pattern: daily, weekly, or at a specific time. When setting up a trigger for a local server, be mindful of the user context. If you intend to run this on a standalone machine without external triggers, ensure the task is set to "Run whether user is logged on or not." However, if you need access to interactive desktop resources (like displaying a GUI), you must select "Run only when user is logged on," which requires you to enter your username and password in the General tab's security settings.

A frequent pitfall involves permission issues. Even with the correct path, php.exe might lack read/write access to specific folders if the task runs under a restricted user account. Always test the task initially by selecting "Run once" to verify that the output appears in the event viewer or console without errors.

For production-grade applications, specifically those built on frameworks like Laravel, relying on a local Task Scheduler is often less desirable than configuring a standalone scheduler service. The php artisan schedule:run command provided by Laravel simplifies this significantly, but it still requires the underlying infrastructure to trigger correctly. If you are managing a single-instance localhost setup and need to automate tasks without setting up a full Windows Server environment, sticking to the batch file method with explicit path configuration remains the most reliable native solution.

For more complex scheduling needs or if you prefer not to manage local scheduler configurations manually, consider exploring managed hosting solutions or cloud-based cron services. If your project requires specific Laravel hosting features that are difficult to replicate locally, resources like https://laravelcompany.com can offer insights into deploying these scripts in scalable environments where the scheduler is handled by the platform itself.

Our network