Accessing SQL Database | PowerShell
Tags: sql, powershell, mssql, sqlconnection
Published: 2022-05-04 17:42:11

---
title: Accessing SQL Database | PowerShell
published: true
date: 2022-05-02 18:03:59 UTC
tags: SQL,Powershell,MSSQL,SQLConnection
canonical_url: 
---

The title couldn’t be more explanatory. During this post, we’ll see how we can access the database, and select data, by PowerShell.

Let me try to guess…

**But why!?**

Well, besides for pure thirst for knowledge of how we can do fancy things :D, could pass through, by using this way of access, to store data through a PowerShell script, like, log entries storage, metrics from system, display the dataset results on a html page, etc.

First things first, so to be able to communicate with the aimed database we need to establish one connection, and for that, I’ll use the following lines…

![](https://toolgbrmaker.files.wordpress.com/2022/05/image.png?w=1005)
_Connecting to the aimed SQL database through System.Data.SqlClient DLL (.SqlConnection)_

A brief explanation of what has been made so far.

On the first line, I’ve built my connection string, adding my SQL server and the database that I’m trying to establish my connection. The server can be easily found by running Microsoft SQL Server Management Studio and on the view tab by selecting registered servers, or by querying the function [@@ServerName](https://docs.microsoft.com/en-us/sql/t-sql/functions/servername-transact-sql?view=sql-server-ver15).

![](https://toolgbrmaker.files.wordpress.com/2022/05/image-1.png?w=318)
_SELECT @@SERVERNAME AS ‘Server Name’_

Afterward, I’m starting to instantiate the DLL that will be used to perform the connection. In the above example, we are using the DLL [System.Data.SqlClient](https://docs.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlconnection?view=dotnet-plat-ext-6.0), after being instantiated will grab the connection string and open/close the communication.

And that’s it, your connection was established and closed (if you gave it a try to run the script shared so far). Now it’s time to get something from this connection. Let’s select some data from our Database.

The query that I’ll within on my PowerShell script is the following:

![](https://toolgbrmaker.files.wordpress.com/2022/05/image-3.png?w=406)
_SELECT * FROM customer WHERE CountryCode = ‘FR’_

Do you remember that I inserted this record in this blog [post](https://dev.to/toolgbrmaker/importing-data-by-csv-sql-i0k)? Looks like I was guessing that he would be elected for the second term of France’s presidency. I’m feeling like an oracle… Do you want to know anything about your future!? ![😀](https://s0.wp.com/wp-content/mu-plugins/wpcom-smileys/twemoji/2/72x72/1f600.png)

Time to pass this query to our PowerShell script. For that I’ve used the following lines of code:

![](https://toolgbrmaker.files.wordpress.com/2022/05/image-4.png?w=598)
_Command that will run on SQL database through System.Data.SqlClient DLL (.SqlCommand)_

A little bit of context of what has been made above. So, we’re still using the [System.Data.SqlClient](https://docs.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlconnection?view=dotnet-plat-ext-6.0) but this time, rather than be establishing the connection, we’re instancing the command that will run on our pre-established connection and store the result dataset on a PowerShell object named **$reader**.

As the last step, we need to ensure that we loop our dataset and print the data returned from our SQL Command. For that I’ve used the following lines of code:

![](https://toolgbrmaker.files.wordpress.com/2022/05/image-5.png?w=630)
_Looping through the Data Set_

Let’s see what our script returns after being executed:

![](https://toolgbrmaker.files.wordpress.com/2022/05/image-6.png?w=1024)
_Our entire script_

Now, let’s pursue excellence… Let’s build a PowerShell function with the above lines, and from that point on, you’ll be able to use parameters that, for example, will get different dataset results based on filter switching. If you’ve doubts of how to do it, please jump to the following blog **[post](https://dev.to/toolgbrmaker/building-a-function-powershell-12d5)** where you’ll get useful hints of the steps to follow.

![](https://toolgbrmaker.files.wordpress.com/2022/05/image-7.png?w=987)
_Our Get-CustomersFromSampleDB PowerShell function_

Let’s invoke the function, once, without parameters, and see what we get.

![](https://toolgbrmaker.files.wordpress.com/2022/05/image-8.png?w=1024)
_Get-CustomersFromSampleDB_

And now by using a country code as a parameter…

![](https://toolgbrmaker.files.wordpress.com/2022/05/image-9.png?w=1024)
_Get-CustomersFromSampleDB -CountryCode ‘PT’_

And I’m happy with what has been achieved. A simple example of how you can connect to your database and get data from it.

I hope that helped! Stay safe!