This is a handy one that I found while googling. Credits to the owner of the script.
These are the steps you need to follow:
1) Stop SQL Server instance
2) Start SQL Server instance in Single user mode
3) Run the Restore command
4) Stop SQL Server instance
5) Start SQL Server instance in Multi user mode
Net stop MSSQLSERVER
Net start MSSQLSERVER /m
sqlcmd -S ServerName\instanceName -E -Q "RESTORE DATABASE databasename FROM DISK='C:\Backup\databasename.bak'"
Net stop MSSQLSERVER
Net start MSSQLSERVER