Featured Post

Thursday, February 8, 2018

TIP: PowerShell script to run all SQL scripts in a folder and in subfolders

You can simply open the “Windows powerShell ISE”.

paste the below script and update the variables as per your folder and SQL details. You need to update ServerName, DatabaseName,"UserName","Password" and Folder Path.


 #Provide SQLServerName
 $ServerName ="ServerName"
 #Provide SQLServerName  
 $ServerName ="ServerName"  
 #Provide Database Name  
 $DatabaseName ="DatabaseName"  
 #Provide Login UserName  
 $UserName = "sa"  
 #Provide Login Password  
 $Password = "Password"  
 #Scripts Folder Path  
 $FolderPath ="E:\Workspace\SqlScripts \"  
 #Loop through the .sql files and run them  
 foreach ($filename in get-childitem -path $FolderPath -recurse -file -filter "*.sql" |  
 sort-object)  
 {  
 invoke-sqlcmd –ServerInstance $ServerName -Database $DatabaseName -Username $UserName -Password $Password -InputFile $filename.fullname  
 #Print file name which is executed  
 $filename  
 }  
And press run button it will execute all your scripts.

1 comment:

  1. Nice blog and absolutely outstanding. You can do something much better but i still say this perfect.Keep trying for the best. Angularjs Development Services

    ReplyDelete