Guides

SQL Server blocked access to procedure 'sys.xp_cmdshell' (Msg 15281): move the command to an Agent PowerShell step

By Scott Coffey, Senior Database Developer. Updated 2026-11-13.

A stored procedure that shells out to copy a file, run a batch file, or call an export tool suddenly fails, often right after a migration to a new server:

Msg 15281, Level 16, State 1, Procedure xp_cmdshell, Line 1
SQL Server blocked access to procedure 'sys.xp_cmdshell' of component 'xp_cmdshell'
because this component is turned off as part of the security configuration for this
server. A system administrator can enable the use of 'xp_cmdshell' by using sp_configure.
For more information about enabling 'xp_cmdshell', search for 'xp_cmdshell' in SQL Server
Books Online.

xp_cmdshell has been off by default since SQL Server 2005. The old server had it turned on; the new one does not. The message tells you how to turn it back on, and that is the fix most people reach for. It works, and it also gives anything running as sysadmin on that instance (an injected query, a compromised login, an AI agent with too much access) a command prompt on the server under the SQL Server service account.

The better fix keeps the operating system work but moves it out of T-SQL and into a SQL Server Agent step that runs as a dedicated, low-privilege Windows account.

Step 1: confirm it is off on purpose

SELECT name, CAST(value_in_use AS int) AS value_in_use
FROM sys.configurations
WHERE name = N'xp_cmdshell';

A value of 0 means it is disabled. Before you change anything, ask who turned it off and why. On many servers a security baseline or audit requires it, and re-enabling it quietly will show up in the next review.

Step 2: find everything that calls it

The procedure that failed is rarely the only caller. Search module definitions in each user database, then Agent job steps in msdb:

-- run in each user database
SELECT OBJECT_SCHEMA_NAME(m.object_id) AS schema_name,
       OBJECT_NAME(m.object_id)        AS object_name
FROM sys.sql_modules AS m
WHERE m.definition LIKE N'%xp[_]cmdshell%';

-- Agent job steps that call it
SELECT j.name AS job_name, s.step_id, s.step_name, s.subsystem
FROM msdb.dbo.sysjobsteps AS s
JOIN msdb.dbo.sysjobs     AS j ON j.job_id = s.job_id
WHERE s.command LIKE N'%xp[_]cmdshell%';

A T-SQL job step that calls xp_cmdshell fails the same way, and its job history shows the step as failed with the Msg 15281 text after "Executed as user". For each hit, write down the actual command, which files or shares it touches, and whether it runs on a schedule or on demand from an application. That tells you what the replacement account needs, and nothing more.

Step 3: create a credential and a proxy

Ask your Windows admin for a domain account that has rights only to the folders and tools the command uses. Then store it in SQL Server as a credential and expose it to Agent as a proxy limited to the PowerShell subsystem. Pass the password as a sqlcmd variable so it never sits in a script file:

-- sqlcmd -S SQL01 -E -i create-proxy.sql -v JobPwd="..."
USE master;
CREATE CREDENTIAL FileExport_Cred
    WITH IDENTITY = N'CONTOSO\svc_fileexport',
         SECRET   = N'$(JobPwd)';

USE msdb;
EXEC dbo.sp_add_proxy
     @proxy_name      = N'PS_FileExport',
     @credential_name = N'FileExport_Cred',
     @enabled         = 1,
     @description     = N'File export jobs only. Replaces xp_cmdshell calls.';

EXEC dbo.sp_grant_proxy_to_subsystem
     @proxy_name     = N'PS_FileExport',
     @subsystem_name = N'PowerShell';

sp_add_proxy checks the credential's identity. If SQL Server cannot resolve it as a Windows account, the proxy is not created and you get Msg 14529, "Proxy ... is not a valid Windows user." Fix the account name (use DOMAIN\account) rather than pointing the credential at a more powerful account that happens to resolve.

If the job owner is not a sysadmin, that login also needs permission to use the proxy:

EXEC msdb.dbo.sp_grant_login_to_proxy
     @proxy_name = N'PS_FileExport',
     @login_name = N'CONTOSO\etl_owner';

Without a proxy, PowerShell and CmdExec steps run as the SQL Server Agent service account, which usually has far more access than one export job needs.

Step 4: move the command into a PowerShell step

Put the logic in a .ps1 file on the server, under version control, and have the step call it. In SQL Rig, agent-job-steps-tsql-powershell.sql builds this job shape: a T-SQL step, a PowerShell step, and a final failure step, created disabled, with @DryRun = 1 by default. Set @ProxyName = N'PS_FileExport' and a step command like this:

$ErrorActionPreference = 'Stop'
& 'D:\Jobs\Export-DailyFile.ps1' -OutputFolder '\\fs01\exports\daily'

Setting $ErrorActionPreference to Stop turns errors inside the script into failures that stop it, so the step reports failure and the job history records the error instead of a misleading success. If you deploy the job script with sqlcmd, avoid raw $(...) subexpressions in the step text; sqlcmd treats $(name) as one of its own variables. Put that logic in the .ps1 file instead.

Two limits to know. SQL Server Agent on Linux supports only T-SQL steps, so set @IncludeOsSteps = 0 there and run the OS work from cron or another scheduler. And SQL Server also has a ##xp_cmdshell_proxy_account## credential for non-sysadmin callers, but it still requires xp_cmdshell to be enabled, so it does not solve the problem this guide is about.

Step 5: start it from T-SQL when the work is on demand

If an application called the procedure and expected the file to appear, start the job instead:

EXEC msdb.dbo.sp_start_job @job_name = N'DBA - Daily file export';

sp_start_job returns as soon as the job is queued; it does not wait for the job to finish, and it does not return the job's result. If the caller needs to know the outcome, have the job write a status row to a table and let the application read that row. A non-sysadmin caller needs to own the job or be in an msdb Agent role that allows starting it, such as SQLAgentOperatorRole. Parameters that the old procedure pasted into a command string are better kept in a small table that the script reads, so no caller can inject text into a command line.

Step 6: audit and keep it off

Once every caller is moved, run SQL Rig's xp-cmdshell-check.sql. It is read-only: it lists xp_cmdshell, OLE Automation, CLR, Ad Hoc Distributed Queries, and related settings with a review flag, plus every enabled proxy, the credential identity behind it, and the subsystems it can use. If xp_cmdshell is still on from the old setup, turn it off:

EXEC sp_configure 'show advanced options', 1; RECONFIGURE;
EXEC sp_configure 'xp_cmdshell', 0;              RECONFIGURE;

Run the job once, check its history with the queries in the Agent job failure guide, and confirm the "Executed as user" line names the proxy account. If the job fails with an owner error before step 1, see the job owner guide.

This matters more when an AI coding agent writes your SQL. Asked to "make the export work," an agent will often suggest enabling xp_cmdshell because the error message says how. SQL Rig's guardrail file lists that as a forbidden pattern and points the agent to an Agent step with a proxy instead.