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 |
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.