Next open a command prompt and connect to your server using sqlcmd using the syntax -S ServerName\InstanceName. You can use the .\ (dot-backslash) syntax to indicate the local server default instance. In the case of SQL Express the instance is called sqlexpress.
https://blogs.msdn.microsoft.com/bethmassi/2008/12/18/attaching-a-local-sql-server-database-using-sqlcmd/
The sqlcmd utility lets you enter Transact-SQL statements, system procedures, and script files at the command prompt, in Query Editor in SQLCMD mode, in a Windows script file or in an operating system (Cmd.exe) job step of a SQL Server Agent job. This utility uses ODBC to execute Transact-SQL batches.
https://docs.microsoft.com/en-us/sql/tools/sqlcmd-utility
When sqlcmd is run from the command line, sqlcmd uses the ODBC driver. Because different default options may apply, you might see different behavior when you execute the same query in SQL Server Management Studio in SQLCMD Mode and in the sqlcmd utility.
https://docs.microsoft.com/en-us/sql/tools/sqlcmd-utility
The SQL File. The below sql script has two variables $database and $Input variable of Where clause. The values will be passed during run time. The sqlcmd utility accepts inputs from a batch file. The batch files simply accept a list of arguments from cmd prompt and allow the program to function based on the order of input parameter.
https://social.technet.microsoft.com/wiki/contents/articles/35120.parameter-s-passing-sql-script-using-batch-file-sqlcmd-powershell-invoke-sqlcmd.aspx
We want to attach the database file to our instance of SQL Server (by the way you can also attach databases to SQL Express). You can do this easily with sqlcmd as outlined in the MSDN library. 1. First copy the local database MDF file to the same location as your other databases.
https://blogs.msdn.microsoft.com/bethmassi/2008/12/18/attaching-a-local-sql-server-database-using-sqlcmd/
We want to attach the database file to our instance of SQL Server (by the way you can also attach databases to SQL Express). You can do this easily with sqlcmd as outlined in the MSDN library. Here are the steps: 1. First copy the local database MDF file to the same location as your other databases.
https://blogs.msdn.microsoft.com/bethmassi/2008/12/18/attaching-a-local-sql-server-database-using-sqlcmd/
Tutorial: sqlcmd Utility. This tutorial provides a brief introduction to the sqlcmd utility for users who do not have experience using a command-line utility. You can use the sqlcmd utility, a Microsoft Win32 command prompt utility, to run ad hoc Transact-SQL statements and scripts.
https://technet.microsoft.com/en-us/library/ms170207(v=sql.105).aspx
If the -U option is used and the -P option is not used, and the SQLCMDPASSWORD environment variable has not been set, sqlcmd prompts the user for a password. If the -P option is used at the end of the command prompt without a password sqlcmd uses the default password (NULL). OSQL -P.
https://social.msdn.microsoft.com/Forums/sqlserver/en-US/15e4a7c0-6547-44e8-9e25-4f14c12570ca/how-to-use-sqlcmd-command-to-login-to-sqlserver-with-sa-account-which-have-empty-password?forum=sqlsecurity
If the -U option is used and the -P option is not used, and the SQLCMDPASSWORD environment variable has not been set, sqlcmd prompts the user for a password. If the -P option is used at the end of the command prompt without a password sqlcmd uses the default password (NULL). Is a user-specified password.
https://social.msdn.microsoft.com/Forums/sqlserver/en-US/15e4a7c0-6547-44e8-9e25-4f14c12570ca/how-to-use-sqlcmd-command-to-login-to-sqlserver-with-sa-account-which-have-empty-password?forum=sqlsecurity
Running commands as another user. Coming to the subject of this article, to run commands as another user, we can use the Invoke-Command cmdlet. However, we need to pass the credentials of the user. We just saw that it is not possible to expand variables inside SetScript.
http://www.powershellmagazine.com/2014/08/13/running-commands-as-another-user-using-dsc-script-resource/
what user does sqlcmd run as
The SQLCMD utility lets you enter Transact-SQL statements, system procedures, and script files at the command prompt, in Query Editor in SQLCMD mode, in a Windows script file or in an operating system job step of a SQL Server Agent job.