T-SQL statement to Enable and Disable XP_CMDSHELL using SP_CONFIGURE in SQL Server

This article outlines the steps to Enable and Disable XP_CMDSHELL using the SP_CONFIGURE system stored procedure.

While running a SQL script that uses XP_CMDSHELL extended stored procedure, are you getting the following error?

Use Master
GO
EXEC sp_configure ‘show advanced options’, 1
RECONFIGURE WITH OVERRIDE
GO
EXEC sp_configure ‘xp_cmdshell’, 1
RECONFIGURE WITH OVERRIDE
GO

ree

Once XP_CMDSHELL is enabled, let’s run the below script to copy files from the source(C:\Azure) to the destination (C:\AzureCopy) directory.

xp_cmdshell ‘copy C:\Azure C:\AzureCopy’;

T-SQL statement to Enable and Disable XP_CMDSHELL using SP_CONFIGURE in SQL Server

The t-SQL script to disable the XP_CMDSHELL in SQL Server is as below:

Use Master
GO
EXEC sp_configure ‘show advanced options’, 1
RECONFIGURE WITH OVERRIDE
GO
EXEC sp_configure ‘xp_cmdshell’, 0
RECONFIGURE WITH OVERRIDE

GO

By Callum

Callum is a news writer at DBBlogger, delivering timely updates and concise analysis across a range of global and digital topics.