Microsoft has an article about this issue. It goes through it all step by step.
https://docs.microsoft.com/en-us/sql/database-engine/configure-windows/connect-to-sql-server-when-system-administrators-are-locked-out
From the Start page, start SQL Server Management Studio. On the View menu, select Registered Servers. (If your server is not already
registered, right-click Local Server Groups, point to Tasks, and then
click Register Local Servers.)
In the Registered Servers area, right-click your server, and then
click SQL Server Configuration Manager. This should ask for permission
to run as administrator, and then open the Configuration Manager
program.
Close Management Studio.
In SQL Server Configuration Manager, in the left pane, select SQL
Server Services. In the right-pane, find your instance of SQL Server.
(The default instance of SQL Server includes (MSSQLSERVER) after the
computer name. Named instances appear in upper case with the same name
that they have in Registered Servers.) Right-click the instance of SQL
Server, and then click Properties.
On the Startup Parameters tab, in the Specify a startup parameter box,
type -m and then click Add. (That's a dash then lower case letter m.)
Note
For some earlier versions of SQL Server there is no Startup Parameters
tab. In that case, on the Advanced tab, double-click Startup
Parameters. The parameters open up in a very small window. Be careful
not to change any of the existing parameters. At the very end, add a
new parameter ;-m and then click OK. (That's a semi-colon then a dash
then lower case letter m.)
Click OK, and after the message to restart, right-click your server
name, and then click Restart.
After SQL Server has restarted your server will be in single-user
mode. Make sure that that SQL Server Agent is not running. If started,
it will take your only connection.
On the Windows 8 start screen, right-click the icon for Management
Studio. At the bottom of the screen, select Run as administrator.
(This will pass your administrator credentials to SSMS.)
Note
For earlier versions of Windows, the Run as administrator option
appears as a sub-menu.
In some configurations, SSMS will attempt to make several connections.
Multiple connections will fail because SQL Server is in single-user
mode. You can select one of the following actions to perform. Do one
of the following.
a) Connect with Object Explorer using Windows Authentication (which
includes your Administrator credentials). Expand Security, expand
Logins, and double-click your own login. On the Server Roles page,
select sysadmin, and then click OK.
b) Instead of connecting with Object Explorer, connect with a Query
Window using Windows Authentication (which includes your Administrator
credentials). (You can only connect this way if you did not connect
with Object Explorer.) Execute code such as the following to add a new
Windows Authentication login that is a member of the sysadmin fixed
server role. The following example adds a domain user named
CONTOSO\PatK.
CREATE LOGIN [CONTOSO\PatK] FROM WINDOWS; ALTER SERVER ROLE
sysadmin ADD MEMBER [CONTOSO\PatK];
c) If your SQL Server is running in
mixed authentication mode, connect with a Query Window using Windows
Authentication (which includes your Administrator credentials).
Execute code such as the following to create a new SQL Server
Authentication login that is a member of the sysadmin fixed server
role.
CREATE LOGIN TempLogin WITH PASSWORD = '************'; ALTER
SERVER ROLE sysadmin ADD MEMBER TempLogin;
Warning:
Replace ************ with a strong password.
d) If your SQL Server is running in mixed authentication mode and you
want to reset the password of the sa account, connect with a Query
Window using Windows Authentication (which includes your Administrator
credentials). Change the password of the sa account with the following
syntax.
ALTER LOGIN sa WITH PASSWORD = '************'; Warning
Replace ************ with a strong password.
The following steps now change SQL Server back to multi-user mode.
Close SSMS.
In SQL Server Configuration Manager, in the left pane, select SQL
Server Services. In the right-pane, right-click the instance of SQL
Server, and then click Properties.
On the Startup Parameters tab, in the Existing parameters box, select
-m and then click Remove.
Note
For some earlier versions of SQL Server there is no Startup Parameters
tab. In that case, on the Advanced tab, double-click Startup
Parameters. The parameters open up in a very small window. Remove the
;-m which you added earlier, and then click OK.
Right-click your server name, and then click Restart.
Now you should be able to connect normally with one of the accounts
which is now a member of the sysadmin fixed server role.