Loop through results of SQL query

I’m trying to create a script to loop through the results of an SQL query and send an email out on a 1-per-line basis. It is a customer list where the customer needs to be updated on the status of their order.

The result set from the SQL query looks like this:
Subject,EmailAddress,Status (Header line from query result)
Sub1,example@example . com,New
Sub2,test@test . com,Aged
Sub3,test@example . com,Closed
Sub4,example@test . com,New

In this example, I would need to send out 4 separate emails.

Email #1:
To: example@example . com
Subject = “Sub1”
Body = “New”

Email #2:
To: test@test . com
Subject = “Sub2”
Body = “Aged”

Email #3:
To: test@example . com
Subject = “Sub3”
Body = “Closed”

Email #4:
To: example@test . com
Subject = “Sub4”
Body = “New”

Individually, I have no problem getting the results back from the query nor sending emails. I do not however know how to loop through the result set and send out all the different emails to the various recipients.

Any help or guidance would be greatly appreciated!

Rob,
Welcome to the forum. :wave:t4:

How do you get the results back from the query. Please share your code.

In general: This forum is for scripting questions rather than script requests. We do not write customized and ready to use scripts or solutions on request.

What have you tried so far? We expect you to make an own attempt first to get your task done or to solve your problem. If you have done so already please document here what exactly you have done and show your code. Then we very likely might be able to help you to step further.

And BTW: When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org <---- Click :point_up_2:t4: :wink: