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—replace MSASxxx with your edition’s version).
  • SQL Server authentication credentials (typically Windows only; use sqlcmd -S localhost for local installs).
  • Command prompt (Admin rights required).

Step-by-Step

  1. Locate your SQL Server CLI tools
    1. Open Command Prompt as Administrator. Navigate to the SQL Server tools folder: cd "C:\Program Files\Microsoft SQL Server\MSASxxx\Tools\Binn" (replace MSASxxx with your edition; e.g., 18.0 for 2019).
    2. Check for sqlservr.exe (for service management) or sqlcmd (for command-line queries). If missing, reinstall SQL Server Services.
  2. Verify SQL Server services are running
    1. Run: net start MSSQLSERVER if the service isn’t running (replace MSSQLSERVER with your instance name if custom; e.g., MSSQL15.MSSQLSERVER for SQL 2017).
    2. Check status with: sc query MSSQLSERVER | find "STATE" (Expected output: RUNNING or PAUSED).
    3. If paused, unpause with: sc config MSSQLSERVER start= auto
  3. Configure firewall rules via CLI (SQL Server Firewall)
    1. 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 the TCPIP library; ensure it’s enabled in sqlcmd -Q "show advanced options".)
    2. 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;" (Replace yourpassword with a Windows password.)
  4. Monitor performance counters via CLI
    1. Check CPU usage: sqlcmd -S localhost -Q "SELECT name FROM sys.dm_os_performance_counters WHERE object_name = 'Process(s)'"
    2. View buffer cache hits: sqlcmd -S localhost -Q "SELECT value FROM sys.dm_os_performance_counters WHERE counter_name = 'Buffer Cache Hits/sec'"

Common Mistakes

  • Missing admin rights: Services need elevated permissions. Try Run as Administrator or check with net start MSSQLSERVER (requires admin).
  • sqlcmd not 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 MSSQLSERVER with your instance name (e.g., MSSQL15.MSSQLSERVER). Check with sc query.
  • Authentication failures: Use Windows auth by omitting credentials or pass a password via -P flag (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 with manage_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 MSSQLSERVER to pause services before altering config.

Troubleshooting

  1. SQL Server won’t start
    1. Check logs: C:\Program Files\Microsoft SQL Server\MSSQLXX\LOG\ERRORLOG (replace XX).
    2. Reset SQL Server service: sc delete MSSQLSERVER sc create MSSQLSERVER binPath= "C:\Program Files\Microsoft SQL Server\MSSQLXX\Binn\sqlservr.exe" start= auto (Replace paths and instance name.)
    3. Restore from backup if corrupted.
  2. Firewall blocks connections
    1. Temporarily disable OS firewall to test: netsh advfirewall set allprofiles state off
    2. Permanently add SQL port in Windows Firewall: netsh advfirewall firewall add rule name="Allow SQL" dir=in protocol=TCP localport=1433 action=allow
  3. No response from sqlcmd
    1. Verify SQL Server is listening: telnet localhost 1433 (Should connect; if not, check firewall or service status.)
    2. Restart SQL Server service: net stop MSSQLSERVER && net start MSSQLSERVER
    3. Check if sqlcmd is in PATH or reinstall SQL Server.

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()"