ICT-Hotlist Topic

How to export a table from Microsoft SQL server 2008r2 to Excel using PowerShell

Before PowerShell you had to use the
Microsoft SQL Query Analyzer to export data to Excel (or query the database from Excel when allowed). This tip compares a simple
query using SQL Query Analyzer versus PowerShell.
The example: get all the records of the table Person.Contact from the AdventureWorks sample database.

Standard Query report

The data can be extracted using the Microsoft SQL Server Management Studio or Microsoft SQL Query
Analyzer by running the query:

Make sure you click the "Results to File" output selection button before running this script.
Save the Report file under the name you select. Next you have to import and format the Report output file for use in Excel.

PowerShell Query version

When using Microsoft SQL Server 2008r2 Management Studio, right click anywhere on the database
tree and select "Start PowerShell" as can be seen in the next image:

Start a Microsoft SQL Server PowerShell command window

This opens a SQL Server PowerShell window that will accept commands right away.The following instruction will:

The file "AdventureWorks.Person.Contact.CSV" is placed on the shared folder
"c:\Share" on the SQL server and can be opened right away using Microsoft Excel 2007 or newer.

Presentation of the table Person.Contact from the AdventureWorks sample database in Microsoft Excel

The SQL Server PowerShell command window closes by typing "exit".
This example can be extended to more complex queries such as joins and where filters.

PowerShell command line version

You can also run Microsoft SQL Server PowerShell scripts on the Command line on a machine where the SQL Server PowerShell plugins are installed. The PowerShell modules are installed by default when installing Microsoft SQL Server. You can manually install the PowerShell Extensions for SQL Server 2012, 2014 or 2016 by installing the following components from the Microsoft® SQL Server® 2016 Feature Pack:

# Open the database connection, get a result set and save it as a Unicode CSV file.Invoke-Sqlcmd-Query$Query-Database$DataBase-ServerInstance$ServerInstance|Export-Csvc:\Share\AdventureWorks.Person.Contact.CSV-encoding "unicode"

You may vote your opinion about this article:

Please leave an optional comment or you can ask a question. (Will not be published)

HTML tags are not allowed.

Do you want to receive an answer? Fill in your e-mail address.(Optional)

Topic created

:

01-05-2017

Topic last edited

:

01-05-2017

Scripts and programming examples disclaimer

Unless stated otherwise, the script sources and programming examples provided are copyrighted freeware.
You may modify them, as long as a reference to the original code and hyperlink to the source page is included in the modified code and documentation.
However, it is not allowed to publish (copies of) scripts and programming examples on your own site, blog, vlog, or distribute them on paper or any other medium, without prior written consent.
Many of the techniques used in these scripts, including but not limited to modifying the registry or system files and settings, impose a risk of rendering the Operating System inoperable and loss of data.
Make sure you have verified full backups and the associated restore software available before running any script or programming example.
Use these scripts and programming examples entirely at your own risk. All liability claims against the author in relation to material or non-material losses caused by the use, misuse or non-use of the information provided, or the use of incorrect or incomplete information, are excluded. All content is subject to change and provided without obligation.