Skip to content

Mike Simmons

My Workbench

  • Home
  • LinkedIn
  • Home
  • Programming
  • PowerShell exporting data easily from MSSQL into a CSV

PowerShell exporting data easily from MSSQL into a CSV

Posted on March 16, 2018March 16, 2018 By Mike No Comments on PowerShell exporting data easily from MSSQL into a CSV
MSSQL, PowerShell, Programming, Windows

PowerShell has a module to assist with this, you can find out all the specifics at https://docs.microsoft.com/en-us/sql/powershell/download-sql-server-ps-module. I was asked to pull data and export it to a CSV to save time, which is easy to do with Task Scheduler and PowerShell. Once you have installed the SqlServer module the website mentioned above will help with that but if you don’t want to read it i’ll make it simple. Install Windows Remote Management version 5+ (I used version 5 at the time 5.1 is out at the time of writing this). You can download version 5 from here. run the following command in an elevated powershell:

Install-Module -Name SqlServer

Once you have done that all you need to do to pull data out of the MSSQL server is

$sqlData = Invoke-Sqlcmd -Query "Your sql query" -Database "Your_Database" -Server "ServerName"

Now to save that data to a CSV file we do:

$sqlData | Export-Csv -NoTypeInformation -Path "Where to save" -Enocoding UTF8

Now all that is left is to crate a Scheduled Task which I will not be covering but will include how to call PowerShell properly in the action tab.

Program/Script: powershell.exe

Add arguments: -ExecutionPolicy Bypass full_path_to_script (surround the path in quotes if it contains spaces)

Complete script:

#Requires -Module SqlServer
$sqlData = Invoke-Sqlcmd -Query "Your sql query" -Database "Your_Database" -Server "ServerName"
$sqlData | Export-Csv -NoTypeInformation -Path "Where to save" -Enocoding UTF8

Tags: MSSQL POWERSHELL PROGRAMMING

Post navigation

❮ Previous Post: PowerShell Tid Bits #1
Next Post: Graylog format AccessList For Windows file events ❯

Leave a Reply Cancel reply

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

I accept that my given data and my IP address is sent to a server in the USA only for the purpose of spam prevention through the Akismet program.More information on Akismet and GDPR.

Pages

  • LinkedIn

Recent Posts

  • PowerShell Anti-Idle Script
  • WSUS MMC Crash Due To XML Invalid Character!
  • Open Maintenance – Docker & CentOS 7
  • Powershell script to create user home directory and DFS namespace target
  • C# save DataTable to CSV easily

Recent Comments

    Archives

    • August 2022
    • March 2018
    • July 2017
    • August 2015

    Categories

    • Active Directory
    • C#
    • CentOS
    • Docker
    • GrayLog
    • MSSQL
    • Networking
    • PowerShell
    • Programming
    • Uncategorized
    • Windows
    • XML

    Meta

    • Log in
    • Entries feed
    • Comments feed
    • WordPress.org

    Copyright © 2023 Mike Simmons.

    Theme: Oceanly by ScriptsTown