Here is a script to restore SQL Server database from bak file command line. This is done via SQLCMD through command line.
The first thing is to open up the command prompt. Once command prompt is open, type in the below query to connect to instance.
C:\> SQLCMD -S localhost\DMSQL2014 -U sa -P sql@2014”
Once connected switch to master database as shown below
The next step is to type in the below command to restore the database.
1> restore database Adventureworks2014 from disk=’e:\ahmad\adventureworks2014.bak’ with stats=10
The database will be restored as shown in below snapshot.
The other way is to do it without connecting to SQLCMD prompt and use –Q parameter of SQLCMD to specify the restore command as shown below.
Another way is to specify a SQL Server script file in –I parameter. The SQL Server script file will have the restore database command.
The file restore.sql has the restore database command. The file is given as input with –I parameter.