Creating multiple sheets in excel using powershell


New Member
Hello Guys,

I saw this archived post here and hence, asking this question:

I have written the following powershell script for getting the database status (offline or online):

foreach ($svr in get-content "demo.txt")


  $con = "server=$svr;database=master;Integrated Security=sspi" 

  $cmd = "select state_desc as DB_STATUS,COUNT(name) as [TOTAL DB'S] from sys.databases group by state_desc"

  $da = new-object System.Data.SqlClient.SqlDataAdapter ($cmd, $con)

  $dt = new-object System.Data.DataTable

  $da.fill($dt) |out-null

  $dt | Format-Table -autosize

The thing here is, I want to change this script in such a way that the output is exported to an excel file and as per the number of servers, new sheets should get created and the output get's stored accordingly, related to the server.
I'm not great at powershell, so kindly help me on this.

My Computer