# SqlCmd vs Invoke-SqlCmd issues with Export-CSV

**URL:** <https://forums.powershell.org/t/sqlcmd-vs-invoke-sqlcmd-issues-with-export-csv/14599>\
**Category:** PowerShell Help\
**Created:** [June 28, 2020, 1:14pm UTC](https://forums.powershell.org/t/sqlcmd-vs-invoke-sqlcmd-issues-with-export-csv/14599 "2020-06-28T13:14:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bailey5](https://avatars.discourse-cdn.com/v4/letter/b/ec9cab/32.png) [@bailey5](https://forums.powershell.org/u/bailey5)\
**Post date:** [June 28, 2020, 1:14pm UTC](https://forums.powershell.org/t/sqlcmd-vs-invoke-sqlcmd-issues-with-export-csv/14599/1 "2020-06-28T13:14:49Z")

</div>

Hi there, I’m new to this forum. I’m somewhat of a novice when it comes to Powershell but have been really getting into it lately. I’ve been wanting to execute some sql commands via powershell, however I’ve had some performance issues with Invoke-SqlCmd and getting the results saved to a file.

Here are my 2 lines:

`Invoke-SqlCmd -ServerInstance "Server" -Database "Database" -Username "User" -Password "Password" -Query $Query | Export-Csv "Output.csv" -NoTypeInformation`

`SqlCmd -S "Server" -d "Database" -U "User" -P "Password" -Q $Query -o "Output.csv" -W -w 2048 -s","`

Server info has been replaced with generic info for example purposes. The query and output location are exactly the same for both pieces of code.

The query returns about 12,000 rows. Here are the duration tests:

| **Type** | **Duration** |
| Invoke-SqlCmd select only | 00:00:17.47 |
| Invoke-SqlCmd + export | **00:11:56:21** |
| SqlCmd select only | 00:00:15.89 |
| SqlCmd + export | 00:00:29.61 |

&nbsp;

&nbsp;

&nbsp;

&nbsp;

&nbsp;

As you can see, exporting to csv takes a very long time for just 12,000 rows at about 25x longer then SqlCmd.Am I doing something wrong? Is it because of the pipe? Is there any way to get around this?

Kind Regards, Bailey.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [June 29, 2020, 2:18am UTC](https://forums.powershell.org/t/sqlcmd-vs-invoke-sqlcmd-issues-with-export-csv/14599/2 "2020-06-29T02:18:40Z")

</div>

I don’t have any experience with SQL but if you just wnat to avoid the pipeline you could try this:

```
$QueryResult = 
Invoke-SqlCmd -ServerInstance "Server" -Database "Database" -Username "User" -Password "Password" -Query $Query 
Export-Csv -Path "Output.csv" -NoTypeInformation -InputObject $QueryResult
```

---

<div class="post-metadata">

**Author:** ![bailey5](https://avatars.discourse-cdn.com/v4/letter/b/ec9cab/32.png) [@bailey5](https://forums.powershell.org/u/bailey5)\
**Post date:** [June 29, 2020, 3:22am UTC](https://forums.powershell.org/t/sqlcmd-vs-invoke-sqlcmd-issues-with-export-csv/14599/3 "2020-06-29T03:22:21Z")

</div>

Hi Olaf,

Unfortunately this does not work, the object that is stored is only the information about the query, not the query itself. The resulting csv looks like this:

| Count | Length | LongLength | Rank | SyncRoot | IsReadOnly | IsFixedLength | IsSynchronized |
| 12810 | 12810 | 12810 | 1 | System.Object[] | FALSE | TRUE | FALSE |

&nbsp;

I’ve read that Export-Csv only works with pipes or single objects (like a query with 1 row for example). I’ve also tried using `| Out-File` but this is just as slow as piping into Export-Csv and is not in the correct format.

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [June 29, 2020, 2:53pm UTC](https://forums.powershell.org/t/sqlcmd-vs-invoke-sqlcmd-issues-with-export-csv/14599/4 "2020-06-29T14:53:12Z")

</div>

I would try it in powershell 7, it fixes several of the windows powershell speed issues.

---

<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:29pm UTC](https://forums.powershell.org/t/sqlcmd-vs-invoke-sqlcmd-issues-with-export-csv/14599/5 "2024-05-16T20:29:19Z")

</div>

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


