Tuesday 5 August 2014

Changing a forgotten SA password using the command line

If you forget the sa password many articles give you advice of how to login to Management Studio and change the sa password. But what if you've inherited someone else's developer PC and you don't have access to Management Studio using your Windows credentials?

There is a way to do it using the command line.

Stop the SQL Server instance
net stop MSSQLSERVER

Start the instance
net start MSSQLSERVER /mSQLCMD

Go in single user mode
sqlcmd -S. -E

1> sp_password @new='<pwd>', @loginame='sa'
2> go

You can also add the user
sp_addsrvrolemember '<domain>\<user>', 'sysadmin'