Export data from Microsoft SQL Server table to CSV using PowerShell

PowerShell can be easily used to export data from Microsoft SQL server table to CSV file. This task can be done by combination of 2 PowerShell cmdlets Invoke-Sqlcmd and Export-Csv:

Invoke-Sqlcmd -ServerInstance . -Database AdventureWorks -Query "SELECT AccountNumber, Name, CreditRating FROM Purchasing.Vendor" | Export-Csv -Path Vendor.csv -NoTypeInformation


Udemy course: Improve your productivity with PowerShell

Content of exported CSV file can be displayed by following PowerShell command:

Get-Content .\Vendor.csv | Select-Object -First 5


"AccountNumber","Name","CreditRating"
"AUSTRALI0001","Australia Bike Retailer","1"
"ALLENSON0001","Allenson Cycles","2"
"ADVANCED0001","Advanced Bicycles","1"
"TRIKES0001","Trikes, Inc.","2"

If you are interested in PowerShell automation, take my Udemy course Improve your productivity with PowerShell.

Leave a Reply

Your email address will not be published. Required fields are marked *