# Run multiple sql scripts in powershell

**URL:** <https://forums.powershell.org/t/run-multiple-sql-scripts-in-powershell/7114>\
**Category:** PowerShell Help\
**Created:** [August 31, 2016, 7:44pm UTC](https://forums.powershell.org/t/run-multiple-sql-scripts-in-powershell/7114 "2016-08-31T19:44:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![danny-ngo](https://avatars.discourse-cdn.com/v4/letter/d/c89c15/32.png) [@danny-ngo](https://forums.powershell.org/u/danny-ngo)\
**Post date:** [August 31, 2016, 7:44pm UTC](https://forums.powershell.org/t/run-multiple-sql-scripts-in-powershell/7114/1 "2016-08-31T19:44:24Z")

</div>

I am trying to create a PowerShell script that runs multiple scripts on a database.  
here is what I have so far. It seems to only work when there is one script but when I add the second script starts to generate errors. Any help will be much appreciated.

```
$crlf = [System.Environment]::NewLine
[string] $TNS = 'LINKED_SERVER'
$sqlfile = "c:\temp\script1.sql", "c:\temp\script2.sql"
$connection=New-Object DATA.OracleClient.OracleConnection("Data Source=$TNS;User Id=TEST;Password=XXXXXXXX")

    foreach ($sqlfiles in $sqlfile){

    $FileLines = Get-Content $sqlfile
    $query = [string]::Join($crlf,$FileLines)
    #log start time
    [datetime]$Now = Get-Date
    [string] $TSbegin = $Now.ToString("yyyy-MM-dd HH:mm:ss")
    echo "`n`nExecuting script $sqlfiles Started at: $TSbegin"
    [System.Reflection.Assembly]::LoadWithPartialName("System.Data.OracleClient") | Out-Null
    
    $connection.Open()
    $res = (new-Object DATA.OracleClient.OracleCommand($query,$connection))
	$output = $res.ExecuteScalar()
     
    #log end time
    [string]$TSend = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
    $Duration = New-TimeSpan -Start ($TSbegin) -End (Get-Date)
    echo "`n$('{0:N0}' -f $output) Rows returned"
    echo "`nExecuting script $sqlfiles completed at: $TSend"
    $Duration |Format-Table -AutoSize 
    # Remove-Variable res
   }

$connection.Close()
```

---

<div class="post-metadata">

**Author:** ![daniel-krebs](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/daniel-krebs/32/484_2.png) [@daniel-krebs](https://forums.powershell.org/u/daniel-krebs)\
**Post date:** [September 1, 2016, 12:16am UTC](https://forums.powershell.org/t/run-multiple-sql-scripts-in-powershell/7114/2 "2016-09-01T00:16:29Z")

</div>

Hi Danny,

I think you’ve got your variable names mixed up (plural vs. singular).

Line 3 you create an array of file names as $sqlfile  
$sqlfile = “c:\temp\script1.sql”, “c:\temp\script2.sql”

Line 6 you iterate over the $sqlfile array and each value in the array gets assigned to $sqlfiles  
foreach ($sqlfiles in $sqlfile){

Line 8 you don’t use the $sqlfiles variable but $sqlfile which contains the array with all filenames. That means Get-Content will return the lines of all files into your $FileLines variable.  
$FileLines = Get-Content $sqlfile

I believe it would be best if you would do the following:

Line 3: Change $sqlfile to $sqlfiles  
$sqlfiles = “c:\temp\script1.sql”, “c:\temp\script2.sql”

Line 6: Swap the variable names around  
foreach ($sqlfile in $sqlfiles){

Example with the suggested modifications:

> <https://gist.github.com/Dan1el42/78d344e873ec1aba85207e0dbfa80c63>

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:40pm UTC](https://forums.powershell.org/t/run-multiple-sql-scripts-in-powershell/7114/3 "2024-05-16T20:40:37Z")

</div>

Closed on May 16, 2024, 8:40pm UTC


