To run SQL Server Configuration Manager commands directly from the command line—without manually opening the GUI—use the `sqlcmd` utility or the Configuration Manager’s integrated CLI tools. This method automates configuration changes, starts services, or checks statuses like firewall rules or performance counters, saving hours over GUI manipulation. You’ll need administrative privileges, and only works on Windows hosts with SQL Server installed. Missing tools? Install them via sqlservr.exe in the SQL Server installation folder.
What You'll Need
- Windows host running SQL Server (2012+ recommended). Labels vary slightly by version—check the official Microsoft documentation if settings differ.
- Administrative access due to service control permissions.
- SQL Server installation path (default:
C:\Program Files\Microsoft SQL Server\MSASxxx\Tools\Binn—replaceMSASxxxwith your edition’s version). - SQL Server authentication credentials (typically Windows only; use
sqlcmd -S localhostfor local installs). - Command prompt (Admin rights required).
Step-by-Step
- Locate your SQL Server CLI tools
- Open Command Prompt as Administrator. Navigate to the SQL Server tools folder:
cd "C:\Program Files\Microsoft SQL Server\MSASxxx\Tools\Binn"(replaceMSASxxxwith your edition; e.g.,18.0for 2019). - Check for
sqlservr.exe(for service management) orsqlcmd(for command-line queries). If missing, reinstall SQL Server Services.
- Open Command Prompt as Administrator. Navigate to the SQL Server tools folder:
- Verify SQL Server services are running
- Run:
net start MSSQLSERVERif the service isn’t running (replaceMSSQLSERVERwith your instance name if custom; e.g.,MSSQL15.MSSQLSERVERfor SQL 2017). - Check status with:
sc query MSSQLSERVER | find "STATE"(Expected output:RUNNINGorPAUSED). - If paused, unpause with:
sc config MSSQLSERVER start= auto
- Run:
- Configure firewall rules via CLI (SQL Server Firewall)
- List existing rules:
sqlcmd -S localhost -Q "EXEC sp_configure 'show advanced options', 1; RECONFIGURE; EXEC sp_configure 'sql server network library', 'TCPIP'; RECONFIGURE"(Note theTCPIPlibrary; ensure it’s enabled insqlcmd -Q "show advanced options".) - Add a firewall rule (e.g., allow port 1433):
sqlcmd -S localhost -U SA -P yourpassword -Q " EXEC sp_configure 'show advanced options', 1; RECONFIGURE; EXEC sp_configure 'protect data from network attacks', 0; RECONFIGURE; EXEC sp_configure 'sql server network library', 'TCPIP'; RECONFIGURE; EXEC sp_configure 'tcp port', 1433; RECONFIGURE; EXEC sp_configure 'tcp dynamic port range start', 0; EXEC sp_configure 'tcp dynamic port range end', 0; RECONFIGURE; EXEC sp_configure 'sql server remote access', 1; RECONFIGURE; EXEC sp_configure 'sql server firewall start port', 1433; EXEC sp_configure 'sql server firewall end port', 1433; RECONFIGURE; EXEC sp_configure 'tcp keepalive', 1; RECONFIGURE;"(Replaceyourpasswordwith a Windows password.)
- List existing rules:
- Monitor performance counters via CLI
- Check CPU usage:
sqlcmd -S localhost -Q "SELECT name FROM sys.dm_os_performance_counters WHERE object_name = 'Process(s)'" - View buffer cache hits:
sqlcmd -S localhost -Q "SELECT value FROM sys.dm_os_performance_counters WHERE counter_name = 'Buffer Cache Hits/sec'"
- Check CPU usage:
Common Mistakes
- Missing admin rights: Services need elevated permissions. Try
Run as Administratoror check withnet start MSSQLSERVER(requires admin). sqlcmdnot found: Ensure it’s in PATH or navigate to the tools folder first. Reinstall SQL Server if missing.- Firewall misconfiguration: Dynamic ports (0–65535) may conflict with OS firewalls. Use static ports (e.g., 1433) for production.
- Service name errors: Replace
MSSQLSERVERwith your instance name (e.g.,MSSQL15.MSSQLSERVER). Check withsc query. - Authentication failures: Use Windows auth by omitting credentials or pass a password via
-Pflag (not recommended for security).
Tips
- Use aliases: Save frequently used commands in a batch file (e.g.,
manage_sql.bat):@echo off cd "C:\Program Files\Microsoft SQL Server\MSAS18\Tools\Binn" sqlcmd -S localhost -Q "EXEC sp_configure 'show advanced options', 1; RECONFIGURE"Call it withmanage_sql.bat. - Redirect output: Capture logs for auditing:
sqlcmd -S localhost -Q "SELECT * FROM sys.databases" > db_list.txt - Test dynamic ports first: Start with port 1433, then adjust dynamically (0–65535) if needed.
- Avoid hardcoding passwords: Use Windows auth or environment variables for secrets.
- Backup before changes: Run
net stop MSSQLSERVERto pause services before altering config.
Troubleshooting
- SQL Server won’t start
- Check logs:
C:\Program Files\Microsoft SQL Server\MSSQLXX\LOG\ERRORLOG(replaceXX). - Reset SQL Server service:
sc delete MSSQLSERVERsc create MSSQLSERVER binPath= "C:\Program Files\Microsoft SQL Server\MSSQLXX\Binn\sqlservr.exe" start= auto(Replace paths and instance name.) - Restore from backup if corrupted.
- Check logs:
- Firewall blocks connections
- Temporarily disable OS firewall to test:
netsh advfirewall set allprofiles state off - Permanently add SQL port in Windows Firewall:
netsh advfirewall firewall add rule name="Allow SQL" dir=in protocol=TCP localport=1433 action=allow
- Temporarily disable OS firewall to test:
- No response from sqlcmd
- Verify SQL Server is listening:
telnet localhost 1433(Should connect; if not, check firewall or service status.) - Restart SQL Server service:
net stop MSSQLSERVER && net start MSSQLSERVER - Check if
sqlcmdis in PATH or reinstall SQL Server.
- Verify SQL Server is listening:
Frequently Asked Questions
How do I run SQL Configuration Manager commands via PowerShell?
PowerShell’s Invoke-SqlCmd replaces sqlcmd. Example:
Invoke-SqlCmd -ServerInstance localhost -Query "SELECT name FROM sys.databases"
Note: Requires Invoke-WebRequest for SQL Server assemblies (install via Install-Module -Name SqlServer).
Can I use this method for Express Edition?
Yes, but with limitations. Express lacks full CLI tools (use GUI or remote SSMS). For advanced tasks, install Standard/Enterprise.
What if I get "Login failed" errors?
Use Windows authentication:
sqlcmd -S localhost -U .\YourDomainAccount
Or omit credentials if using Windows auth (SQL Server uses the local Windows user). Avoid plaintext passwords.
How do I check if dynamic ports are enabled?
Run:
sqlcmd -S localhost -Q "EXEC sp_configure 'tcp dynamic port range start', 'tcp dynamic port range end'"
Dynamic ports default to 0–65535; set to 0–0 to disable (static port only).
Can I automate these commands in a script?
Absolutely. Example script (save as sql_config.bat):
@echo off
cd "C:\Program Files\Microsoft SQL Server\MSAS18\Tools\Binn"
sqlcmd -S localhost -Q "EXEC sp_configure 'show advanced options', 1; RECONFIGURE"
net stop MSSQLSERVER
net start MSSQLSERVER
pause
Run with elevated privileges and include error handling (e.g., check %ERRORLEVEL%).
What if I need to modify a specific instance (not localhost)?
Replace localhost with the server name or IP:
sqlcmd -S sql_server_name -U SA -P password -d master
For named instances (e.g., .\SQLEXPRESS), use:
sqlcmd -S .\SQLEXPRESS -Q "SELECT DB_NAME()"