<<< Back to Search

Database

Execute SQL Script using Connection String

Execute SQL Script for a given Server/Database on Trusted Connection using Connection String

# Parameters
$scriptPathFile = "C:\Temp\SQLScript.sql"
$databaseServer = "DESKTOP-xxxx"
$database = "master"

# Custom connection string based on Trusted connection
$connectionString = "Server=$databaseServer;Database=$database;Trusted_Connection=True;"
    
# Execute SQL Script -_- 
Invoke-Sqlcmd -InputFile $scriptPathFile -ConnectionString $connectionString -Verbose

PS C:\>

TimeOfScript       
------------       
4/3/2021 9:35:50 AM

Execute SQL Script using Named Instance

Execute SQL Script for a given Server/Database on Trusted Connection using Named Instance

# Parameters
$scriptPathFile = "C:\Temp\SQLScript.sql"
$serverInstance = "DESKTOP-XXXXX"
$database = "master"

   
# Execute SQL Script -_-  
Invoke-Sqlcmd -InputFile $scriptPathFile -ServerInstance $serverInstance -Database $database -Verbose

PS C:\>

TimeOfScript       
------------       
4/3/2021 9:35:50 AM