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?


XP_CMDSHELL Blocked access 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

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


17 views0 comments