Invoke-Sqlcmd cmdlet is used to run sql commands. We will see in this blog on how we can run large SQL scripts file through PowerShell.

1. Read SQL Script in a variable.

   1: $sql = [Io.File]::ReadAllText('.\CreateMyDBTables.sql');

2. Import SQLPS module

   1: Import-Module 'SQLPS' -DisableNameChecking;

3. Invoke the script. Specify Server Instance, I have specified local instance in below example

   1: Invoke-Sqlcmd -Query $sql -ServerInstance '.';

 

The full PowerShell script will look like below:-

   1: $sql = [Io.File]::ReadAllText('.\InstallPortalDB.sql');
   2: Import-Module 'SQLPS' -DisableNameChecking;
   3: Invoke-Sqlcmd -Query $sql -ServerInstance '.';

Happy Scripting!!!