Skip to content

Instantly share code, notes, and snippets.

@djhojd
Created August 5, 2020 18:15
Show Gist options
  • Save djhojd/6ea64529c11e72abfdb67045439b6030 to your computer and use it in GitHub Desktop.
Save djhojd/6ea64529c11e72abfdb67045439b6030 to your computer and use it in GitHub Desktop.
$MachineName = $env:computername
$InstanceName = "SQLEXPRESS"
$ServerInstance = "$MachineName\$InstanceName"
Get-SqlInstance -ServerInstance $ServerInstance
# Provide SQLServerName
$SQLServer = "MySQLSErver\InstanceName"
# Provide Database Name
$DatabaseName = "my-db-name"
# Scripts Folder Path
$FolderPath = "c:\sql-files\"
# Loop through the .sql files and run them
foreach ($filename in get-childitem -path $FolderPath -filter "*.sql")
{
invoke-sqlcmd -ServerInstance $ServerInstance -Database $DatabaseName -InputFile $filename.fullname
# Print file name which is executed
$filename
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment