PowerShell Script to Query SQL Servers(s) and output to Excel


SQL Notes From The Underground

Ive been doing a lot of work with data ending up in Excel recently, so wanted to make a script that made it a bit quicker to get from A to B.

What i managed to come up with is pretty close to what i wanted. there’s one issue i know about that im not sure how to fix, which is to get the dynamic headers, i read from

which puts the headers in Alphabetical order instead of the order they come out of the query. so you either need to make sure the SELECT statements col are in that order, or comment it out.

Im thinking of adding few extra bits onto it like

  • Login, (I work in a multi domain environment so having some logic to choose which login account to use could be handy)
  • Check the connection is valid before running the query, maybe even check the…

View original post 49 more words