Just Code‎ > ‎

Powershell - Use SQLCMD to run all scripts in a folder on one target database

posted Dec 20, 2011, 5:24 AM by Peter Henell   [ updated Jan 25, 2012, 5:16 AM ]
Homework: make it run on every database on the server
cls
[System.IO.DirectoryInfo]$workdir = New-Object System.IO.DirectoryInfo("C:\src\database")
$files = $workdir.GetFiles() | where {$_.Extension -eq ".sql"}

Set-Location SQLSERVER:\SQL\Localhost\DEFAULT\Databases\CustomerDB

foreach( $file in $files)
{
    Invoke-Sqlcmd -InputFile $file.FullName
    #Write-Host $file.Name
}
Comments