Performing bulk export using Invoke-SqlCmd

This recipe shows how to export contents of a table to a CSV file using PowerShell and the Invoke-SqlCmd cmdlet.

Getting ready

Make sure you have access to the AdventureWorks2014 database. We will use the Person.Person table. Create a C:\Temp folder, if you don't already have it in your system.

How to do it...

Follow these steps to perform a bulk export using PowerShell and Invoke-sqlcmd:

  1. Open PowerShell ISE as administrator.
  2. Import the SQLPS module and create a new SMO Server Object:
    #import SQL Server module Import-Module SQLPS -DisableNameChecking #replace this with your instance name $instanceName = "localhost" $server = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server -ArgumentList $instanceName ...

Get SQL Server 2014 with PowerShell v5 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.