SQL Server Express has no SQL Server Agent, and that is still the case in SQL Server 2025. To schedule index maintenance, backups or any other recurring job on Express, use Windows Task Scheduler to run a batch file that calls sqlcmd. Two details decide whether it works unattended: point sqlcmd at the Express instance name, and add -b so a failed run shows up as a failed task.
Topics Covered:
– Pre-requisites: Install SQLCMD
– Create a SQLCMD Script for Scheduling
– Create Scheduled Task for SQLCMD Script
Pre-requisites: Install SQLCMD
Before proceeding, ensure that sqlcmd is installed. Microsoft’s page Download and install the sqlcmd utility covers both versions that exist now:
- sqlcmd (ODBC): the long-standing one, installed with SQL Server and with the Microsoft Command Line Utilities.
- sqlcmd (Go): a newer standalone download, for example
winget install sqlcmd. It takes the same common switches.
A SQL Server install puts the ODBC version in this directory:
C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\<version>\Tools\Binn

On my lab machine with SQL Server 2025 that is ODBC\180\Tools\Binn, and sqlcmd -? reports version 17.0. Run where.exe sqlcmd to see which copy your PATH picks up. Checkout this Microsoft link for more information on file paths for specific SQL apps.
Encryption in newer sqlcmd versions
ODBC sqlcmd 18 and later encrypts the connection by default and checks the server certificate. Against an instance still using its self-signed certificate, it fails before running anything. This is the real output from my lab instance:
Sqlcmd: Error: Microsoft ODBC Driver 18 for SQL Server : SSL Provider: The certificate chain was issued by an authority that is not trusted. Sqlcmd: Error: Microsoft ODBC Driver 18 for SQL Server : Client unable to establish connection.
Adding -C trusts the server certificate and the same command connects. That is fine for a local Express instance; on anything shared, give the instance a certificate the client trusts instead, as in Forcing Encrypted Connections in SQL Server Using Certificates. The Go version only validates the certificate when you pass -N, so it connects to the same instance without -C.
Create a SQLCMD Script for Scheduling
I’m going to create a new sqlcmd script that will perform index maintenance and update statistics for all the databases on the instance. It calls Ola Hallengren’s IndexOptimize, which needs installing first; his install script puts it in master by default.
sqlcmd -S .\SQLEXPRESS -E -C -b -d master -Q "EXEC dbo.IndexOptimize @Databases = 'ALL_DATABASES', @FragmentationLow = NULL, @FragmentationMedium = 'INDEX_REBUILD_OFFLINE', @FragmentationHigh = 'INDEX_REBUILD_OFFLINE', @FragmentationLevel1 = 30, @FragmentationLevel2 = 50, @UpdateStatistics = 'ALL', @OnlyModifiedStatistics = 'Y'" -o "C:\logs\index_maintenance.txt"
What each switch does: -S .\SQLEXPRESS : Express installs as a named instance called SQLEXPRESS by default. Without -S, sqlcmd tries the default instance on the local machine, which an Express-only server doesn’t have. -E : Windows authentication, as the account the task runs under. -C : Trust the server certificate (see above). -b : Exit with error level 1 when SQL Server returns an error. -d master : The database holding IndexOptimize. -o : The output file. It is overwritten on every run.
The screenshots below show the original 2019 version of this command. IndexOptimize has since dropped the @PageCountLevel parameter (it is @MinNumberOfPages now, default 1000 pages), and the statistics update now happens in IndexOptimize itself with @UpdateStatistics, so use the line above.
Next, I save the command in Notepad as a .bat file, for example index_maintenance.bat.


I’m going to create a new folder for the output log file too:

Before scheduling the script, we should run it manually to ensure it executes correctly:


If this is successful, the output will be saved in C:\logs\index_maintenance.txt.
Why the -b Switch Matters
Without -b, sqlcmd exits with 0 even when the SQL inside it fails, and Task Scheduler reports the run as a success. I tested it on the lab instance with IndexOptimize missing from master. Both runs wrote this to the log file:
Msg 2812, Level 16, State 62, Server HPAI01, Line 1 Could not find stored procedure 'dbo.IndexOptimize'.
Without -b the batch file returned exit code 0; with -b it returned 1. Run from a scheduled task, the version with -b recorded a Last Run Result of 1, so the failure is visible without opening the log. If you do see error 2812, the procedure is either not installed or not in the database -d points at, and Could Not Find Stored Procedure (Error 2812) on sqldba.blog walks through finding it.
Create a Scheduled Task for SQLCMD Script
Now that the script is ready and tested, we can automate it using Windows Task Scheduler.
Step 1: Open Windows Task Scheduler

Step 2: Create New Scheduled Task

>> General Tab
– Provide a meaningful name, e.g., SQL Index Maintenance.
– Set the user account for execution (ensure it has “Log on as a batch job” permissions). The account also needs a login on the Express instance with enough rights to run the maintenance.
– Select Run whether user is logged on or not, so the task runs with nobody signed in.

>> Triggers Tab
– Set the schedule (e.g., Weekly at 2 AM on Sundays).


>> Actions Tab:
– Choose Start a Program.
– Browse to and select the .bat file created earlier.


>> Settings Tab:
– Configure additional options such as retry attempts and stopping the task if it runs too long.

Step 3: Test and Verify the Task
Manually run the scheduled task by right-clicking and selecting Run. Then check two things: the Last Run Result column, and the log file the script writes.
Manual Task Execution:
When you run a task manually, you’ll be prompted to enter a username and password if the task is set to use an AD service account.

Scheduled Task Execution History:
After running the new Scheduled Task manually, I can see it ran successfully within the history:

A Last Run Result of The operation completed successfully. (0x0) plus a clean log file is a good run. (0x1) means sqlcmd hit an error: a failed connection, or an error from the SQL itself, which only reaches Task Scheduler because of -b. The log file says which.
For other ways to schedule work on Express, sqldba.blog has Schedule Jobs in SQL Server Express Without SQL Agent, and sqlcmd Examples for SQL Server covers more of the switches used here.
Leave a Reply