How to Execute and Store Transact-SQL (T-SQL) Query Results in a PowerShell Array

September 12, 2017
Read this post to learn how you can execute and store Transact-SQL (T-SQL) queries results in a PowerShell array.
How to Execute and Store Transact-SQL (T-SQL) Queries Results in A PowerShell Array


By: Jharred R. Williams
 
 
 

A brief intro of Windows PowerShell

• Windows Powershell is a task automation framework used for its ease of use when compared to building full blown executables.

• With a command line shell that is almost identical to a command window, the tool has an interactive editor that is similar to SQL Server Management Studio’s intellisence for automatic completion of statements.

• The scripting language is based on the .NET Framework and all of its classes, methods, and different data types.

• With a simple UI experience, Powershell is as its name implies: powerful. Users can do .NET programming from a Powershell command without having to build or package anything, giving users access to COM objects and WMI controls.

This also allows for 2 key capabilities:

→ First, it allows for admin task – the core principle of exactly what the tool does.

→ Secondly, it allows users to execute scripts both locally and on remote machines.

 

Windows PowerShell ISE

I prefer to use the Windows PowerShell ISE when working with PowerShell. It can be searched and opened from the Windows Start menu.

ISE is an acronym for “Integrated Scripting Environment” and it contains the following:

• A command shell

• A command builder that allows users to build commands using a form based tool

• An integrated helper

• A script editor with a debugger

 

Cmdlets

Cmdlets are at the heart of PowerShell and follow a verb-noun convention, as shown here in these 3 examples.

• Get-ChildItem

• Set-Date

• Format-List

So now that we have background knowledge on the tool, let’s get down to business.
 

Executing T-SQL queries from the PowerShell ISE

Step 1: Build your connection string to the server and database in which you would like to execute your query.

 

Step 2: Insert your T-SQL query into the $SqlCmd.CommandText space.

 

Step 3: Execute the Powershell script and view them in the Windows Powershell ISE.

Nice work!

In case you were curious like me, a stored procedure with parameters can be executed in a very similar fashion. Let’s continue.
 

Pulling your T-SQL Query results into a PowerShell array

Building upon the previous 3 steps, users can pump their query results into an array by making a few changes to the script below. At this point we need to set the results to a variable. This is illustrated below by the variable named $Data.

Excellent work today!  I will demonstrate how you can begin tapping further into the .NET framework once your data is in an array  in part 2 of this blog post.

 
 
 


Key2 Consulting is a data warehousing and business intelligence company located in Atlanta, Georgia. We create and deliver custom data warehouse solutions, business intelligence solutions, and custom applications.

Related Content
Rebuilding Indexes Based on Partition 

Rebuilding Indexes Based on Partition 

Author: Austin Dolezal Many businesses are wasting time and money cleaning up their data when small fixes and tweaks could optimize their processing time and storage space.  Database indexes and partitions are often created and...

5 Key Takeaways From PASS Data Community Summit 2022

5 Key Takeaways From PASS Data Community Summit 2022

The PASS Data Community Summit 2022 was a blast! A handful of people from our company attend the conference every year and this year was no exception. The knowledge gained, connections made, and fun enjoyed in Seattle is always worth the investment. Here are our five...

How to Delete Data From a Large Partitioned Table

How to Delete Data From a Large Partitioned Table

By: Ken Adams   The Problem I recently ran across an issue on a project that required a large amount of data to be removed from a table, specifically an issue with the loading of the table causing duplication of data. All non-clustered indexes had been removed...