title: How to export to csv every write-host?
body: <p>I have the following script that displays the connections of each database in the server</p>
  
  <p>I want to create and export to csv the "server", "database", and "connection info" column headers, and basically output the server name,DBName,and connection information to corresponding column. </p>
  
  <p>I looked into <code>PSCustomObject</code> but i am not sure how to implement this exactly in my script  </p>
  
  <pre><code>write-host "`r`n            Server            |               DataBase               |       Connection info      `r`n" -foregroundcolor white -backgroundcolor darkyellow
  
  Import-Module SqlServer
  
  $oldAS = New-Object Microsoft.AnalysisServices.Server
  $oldAS.connect("server1")
  
  foreach ($db in $oldAS.Databases){
      $compatibility_level = $db.CompatibilityLevel
      if($compatibility_level -lt 1200)
      {
        $OLDdbName = $db.Name
        Write-Host $OLDdbName -Fore green
  [PSCustomObject]@{
      DataBase = $OLDdbName
  }
        $db.DataSources | ForEach-Object{write-host "$($_.ConnectionString)";
      [PSCustomObject]@{
          "Connection Info" = $_.ConnectionString
                }
  }
      }
      else
      {
          $AS = new-Object Microsoft.AnalysisServices.Tabular.Server
          $AS.Connect("server1")
  
          foreach ($dbt in $AS.Databases | Where-Object{$compatibility_level -ge 1200} ){
            $dbName = $dbt.Name
            Write-Host $dbName
     [PSCustomObject]@{
          DataBase = $dbName
      }
            if(($dbt.model.datasources[0]).GetType().Name -match "ProviderDataSource") 
            {
              write-host "$($dbt.model.datasources[0].ConnectionString)"
              }
            else {
                  #$dbt.model.datasources[0].ConnectionDetails.ToString(); #ToJson
                  write-host "$($dbt.model.datasources[0].Credential.ToString())"
     [PSCustomObject]@{
          "Connection Info" = "$($dbt.model.datasources[0].Credential.ToString())"
      }
              }
          }
      }
  } | Export-Csv -Path .\CONNECTIONS_LOG.csv -NoTypeInformation -Append
  </code></pre>
  
  <p>EDIT:</p>
  
  <pre><code>Import-Module SqlServer
  
  $oldAS = New-Object Microsoft.AnalysisServices.Server
  $oldAS.connect("server1")
  
  #create .net array object
  $exportObject = New-Object System.Collections.ArrayList
  
  #create ordered dictionary so column names
  #come out in the ordered they were created
  $rowObject = [ordered]@{}
  
  foreach ($db in $oldAS.Databases){
      $compatibility_level = $db.CompatibilityLevel
      if($compatibility_level -lt 1200)
      {
        $OLDdbName = $db.Name
        Write-Host $OLDdbName -Fore green
        $db.DataSources | ForEach-Object{
        write-host "$($_.ConnectionString)"
  
        $rowObject.'Connection' = $($_.ConnectionString)
        $exportObject.Add((New-Object PSObject -Property $rowObject)) | Out-Null
        }
  
        $rowObject.'Database' = $OLDdbName 
        $exportObject.Add((New-Object PSObject -Property $rowObject)) | Out-Null
      }
      else
      {
          $AS = new-Object Microsoft.AnalysisServices.Tabular.Server
          $AS.Connect("server1")
  
          foreach ($dbt in $AS.Databases | Where-Object{$compatibility_level -ge 1200} ){
            $dbName = $dbt.Name
            Write-Host $dbName
  
            $rowObject.'Database' = $dbName
              $exportObject.Add((New-Object PSObject -Property $rowObject)) | Out-Null
  
            if(($dbt.model.datasources[0]).GetType().Name -match "ProviderDataSource") 
            {
              write-host "$($dbt.model.datasources[0].ConnectionString)"
                  $rowObject.'Connection' = $($dbt.model.datasources[0].ConnectionString)
                  $exportObject.Add((New-Object PSObject -Property $rowObject)) | Out-Null
              }
            else {
                  #$dbt.model.datasources[0].ConnectionDetails.ToString(); #ToJson
                  write-host "$($dbt.model.datasources[0].Credential.ToString())"
  
                  $rowObject.'Connection' = $($dbt.model.datasources[0].Credential.ToString()) 
                  $exportObject.Add((New-Object PSObject -Property $rowObject)) | Out-Null
              }
          }
      }
  }
  
  $exportObject | Export-CSV "C:\temp\dbinfo.csv" -NoTypeInformation -Encoding UTF8 -Delimiter ','
  </code></pre>