title: Exporting info from PS script to csv
body: <p>This is a powershell/AD/Exchange question....</p>
  
  <p>I'm running a script against a number of Users to check some of their attributes however I'm having trouble getting this to output to CSV. The script runs well and does exactly what I need it to do, the output on screen is fine, I'm just having trouble getting it to directly export to csv.</p>
  
  <p>The input is a comma seperated txt file of usernames (eg "username1,username2,username3")</p>
  
  <p>I've experimented with creating custom ps objects, adding to them and then exporting those but its not working.... </p>
  
  <p>Any suggestions gratefully received..</p>
  
  <p>Thanks</p>
  
  <p>George</p>
  
  <pre><code>$array = Get-Content $InputPath
  
  #split the comma delimited string into an array
  $arrayb = $array.Split(",");
  
  foreach ($User in $arrayb)
  {
      #find group memebership
      Write-Host "AD group membership for $User"
      Get-QADMemberOf $User
  
      #Get Mailbox Info
      Write-Host "Mailbox info for $User"
      Get-Mailbox $User | select ServerName, Database, EmailAddresses, PrimarySmtpAddress, WindowsEmailAddress
  
      #get profile details
      Write-Host "Home drive info for $User"
      Get-QADUser $User| select HomeDirectory,HomeDrive
  
      #add space between users
      Write-Host ""
      Write-Host "******************************************************"
  
  
  }
  
  Write-Host "End Script"
  </code></pre>
  
  <p>EDITED....
  Methods I have tried for exproting (showing only the for loop/export code)
  Method1</p>
  
  <pre><code>$AllData = @()  
  
  foreach ($User in $arrayb)
  {
      #set title for this user
      #Write-host "Details for $User"
  
      #find out their group memebership
      Write-Host "AD group membership for $User"
      $AdMemberOf = Get-QADMemberOf $User 
      Write-Host "ad completed"
      Write-Host ""
      Write-Host ""
  
  
      #Get Mailbox Info
      Write-Host "Mailbox info for $User"
      $ExInfo = Get-Mailbox $User | select ServerName, Database, EmailAddresses, PrimarySmtpAddress, WindowsEmailAddress
      Write-Host "ex completed"
      Write-Host ""
      Write-Host ""
  
  
      #get profile details
      Write-Host "Home drive info for $User"
      $HomeInfo = Get-QADUser $User| select HomeDirectory,HomeDrive
      Write-Host "home drive completed"
  
      #add space between users
      Write-Host ""
      Write-Host "******************************************************"
  
  
      $ReturnedObj = New-Object PSObject
      $ReturnedObj | Add-Member NoteProperty -Name "AD Group Membership for $User" -Value $AdMemberOf
      $ReturnedObj | Add-Member NoteProperty -Name "Exchange details for $User" -Value $ExInfo
      $ReturnedObj | Add-Member NoteProperty -Name "Home drive info for $User" -Value $HomeInfo
  
  
      Write-Host $ReturnedObj
  
      $AllData += $ReturnedObj
  
  
  }
  Write-Host "starting csv export"
  Write-Output $AllData |export-csv -Path $OutputPath -notype -force
  Write-Host "End Script"
  </code></pre>
  
  <p>Method2</p>
  
  <pre><code>$ExportInfo = @()
  
  
  foreach ($User in $arrayb)
  {
      $UserInfo = New-Object System.Object
  
      #set title for this user
      $ExportInfo += "Details for $User"
  
      #Get Mailbox Info
      $ExportInfo += Get-Mailbox $User
  
      #find out their group memebership
      $ExportInfo += Get-QADMemberOf $User 
  
      #get profile details
      $ExportInfo += Get-QADUser $User| select HomeDirectory,HomeDrive
  
      #add space between users
      $ExportInfo += ""
      $ExportInfo += ""   
  
  
  }
  
  $ExportInfo | Export-Csv -Path $OutputPath ;
  </code></pre>
  
  <p>EDITED:</p>
  
  <p>Thanks to suggestion from Stej I now have the code below... Still doesn't work correctly. I have added a check to confirm the user exists in AD as well. First problem is with getting a users AD group membership - if I put a break point in and look at the value of a a specific users AD membership, the value in teh varialbe is error "Cannot process argument because the value of argument "obj" is null. Change the value of argument "obj" to a non-null value." No idea what's causing this, so I've just ignored it for now and commented out those lines for AD membership to concentrate on gettng the rest working. </p>
  
  <p>With the lines commented out, the script runs fine and again, with a breakpoint at the bottom I can look at the value of $ExportInfo and they all look fine, ie they have all been saved into the variable correctly. However, it won't output. As you can see, I put a command to get $ExportInfo written to screen but that doesn't show anything. When it attempts to execute the Export line (ie "Export-Csv $ExportInfo -Path $OutputPath") it errors with:
   "Cannot convert 'System.Object[]' to the type 'System.Char' required by parameter 'Delimiter'. Specified method is not supported.
  At :line:80 char:10
  + Export-Csv &lt;&lt;&lt;&lt;  $ExportInfo -Path $OutputPath"</p>
  
  <p>I chnaged the line to "$ExportInfo | Export-Csv -Path $OutputPath" and it now exports to CSV... No idea why??? Two issues though... as noted above, AD groups aren't working and the field Email Addresses (which should return something like { SMTP:j.smith@domain.com, smtp: j.smith@domain.com.au, smtp: smithj@exchange.domain.com.au and SIP:j.smith@domain.com.au}) just shows up in the CSV as "Microsoft.Exchange.Data.ProxyAddressCollection". Again, checking in $ExportInfo, the addresses are there....</p>
  
  <p>Thanks</p>
  
  <pre><code>$ExportInfo = @()
  
  foreach ($User in $arrayb)
  {
      $CheckUser = Get-QADUser -Name $User
  
      if (!$CheckUser)
      {
          $CountUser++
          Write-Warning "############################################"
          Write-Warning "$user not found in AD"
          Write-Warning "############################################"
      }
      else
      {
          $CountUser++
  
          $UserInfo = New-Object System.Object
  
  
  
          #find out their group memebership
          Write-Host "AD group membership for $User"
          #$Temp = Get-QADMemberOf $User 
          #$UserInfo | Add-Member NoteProperty -Name "AD Group Membership" -Value $Temp.Name
  
          #set title for this user
          #Write-host "Details for $User"
  
          #Get Mailbox Info
          Write-Host "Mailbox info for $User"
          $Temp = Get-Mailbox $User #| select ServerName, Database, EmailAddresses, PrimarySmtpAddress, WindowsEmailAddress
          $UserInfo | Add-Member NoteProperty -Name "ServerName" -Value $Temp.ServerName
          $UserInfo | Add-Member NoteProperty -Name "Database" -Value $Temp.Database
          $UserInfo | Add-Member NoteProperty -Name "Email Addresses" -Value $Temp.EmailAddresses
          $UserInfo | Add-Member NoteProperty -Name "Primary SMTP" -Value $Temp.PrimarySmtpAddress
          $UserInfo | Add-Member NoteProperty -Name "Windows Email Address" -Value $Temp.WindowsEmailAddress
          #$ReturnedObj | Add-Member NoteProperty -Name
  
  
  
  
  
          #get profile details
          Write-Host "Home drive info for $User"
          $Temp = Get-QADUser $User #| select HomeDirectory,HomeDrive
          $UserInfo | Add-Member NoteProperty -Name "Home Directory Location" -Value $Temp.HomeDirectory
          $UserInfo | Add-Member NoteProperty -Name "Home Drive Mapped To" -Value $Temp.HomeDrive
  
          #add space between users
          Write-Host ""
          Write-Host "******************************************************"
  
          $ExportInfo += $UserInfo
      }#end else
  
  }
  Write-Host "blah"
  Write-Host $ExportInfo
  Export-Csv $ExportInfo -Path - $OutputPath
  
  Write-Host "Number of Users processed: $CountUser"
  </code></pre>