This guide explains how to regain `sysadmin` privileges in SQL Server using `sqlcmd` and single-user mode.
On the host machine:
net stop MSSQLSERVER
(Replace `MSSQLSERVER` with your instance name if not default.)
Launch from an elevated command prompt:
cd "C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\Binn\" .\sqlservr -m
*Adjust the path to your instance version and install location.*
Leave this window running.
sqlcmd -S localhost -E
CREATE LOGIN [MACHINE_NAME\Administrator] FROM WINDOWS; GO ALTER SERVER ROLE [sysadmin] ADD MEMBER [MACHINE_NAME\Administrator]; GO EXIT
Replace `MACHINE_NAME` with your actual computer name. You can confirm it with `whoami`.
In another terminal:
net stop MSSQLSERVER net start MSSQLSERVER
You should now have full access using Windows Authentication.
Don’t forget the GO statements — each batch must be terminated for `sqlcmd` to execute it.