title: How do I get Powershell to output Get-ADUser -Filter * as comma delimited?
body: <p>I have a simple Powershell script that I want to use to grab all the users in AD and show specific properties.  Here is the heart of my script:</p>
  
  <pre><code>$id = "*"
  Get-ADUser -Filter {SAMAccountName -like $id} -Properties * | Select-Object -Property SAMAccountName,Name,PasswordNeverExpires,LockedOut,PasswordLastSet,LastLogOnDate,CanonicalName 
  </code></pre>
  
  <p>The full script has an input parameter to set $id so that it can be a single ID, a list of IDs (such as "bsmith, jdoe, gmanning"), or * to get all accounts.  </p>
  
  <p><strong>Here's my problem:</strong> When using, *, I need this to output as comma delimited.</p>
  
  <p><strong>Here's the catch:</strong>  The output cannot be to a CSV file--or any other type of file.</p>
  
  <p>The reason being is that I'm writing this script to be used on N-Enable's N-Central monitoring suite of software.  You don't need to know N-Central to help with my problem, just understand that N-Central uses its own software to run Powershell scripts on clients and returns the results into a txt file that cannot be changed to a csv file (or formatted in any other way than what it has hard-coded). </p>
  
  <p>What this means is that my results have to either be what would show up on the screen or in a variable (such as $results=Get-ADUser -Filter * ....).  I cannot output to any type of file, which leaves out Export-CSV as an option.</p>
  
  <p>I've tried other types of formatting to no avail  (such as with -f).  The issue seems to be with the way Powershell grabs all AD Users using the * wildcard.  It seems to grab them all as one big object, so I am unable to get my output to have a comma between all the properties so that I get a comma-delimited output. Thus, when I get the results back from N-Central as a .txt file, all the data is there, but there are no commas in between the properties for me to then open the .txt file as comma-delimited in Excel (or tab-delimited for that matter). </p>
  
  <p>Does anyone have a solution that will allow me to format Get-ADUser -filter * so that it is comma-delimited without using export to file?</p>
  
  <p><strong>UPDATE:</strong> Ok, I thought I was keeping things easy by not posting my full script but it seems I've done the opposite. So, below is my full script.  Anyone should be able to run this to see the results:</p>
  
  <pre><code>function Get-ADUserInfo
  {
      [CmdletBinding()]
      param(
          #[Parameter(Mandatory=$True)]
          [string[]]$Users= '*'
  
      )
      Begin {
  
          $maxPasswordAge = (Get-ADDefaultDomainPasswordPolicy).MaxPasswordAge.Days
          $Headers="ID, Name, Password Never Expires?, Locked Out, Password Last Set, Expiry Date, Last Logon Date, OU Path"
  
      }
      Process {
          foreach ($id in $Users)
          {
              $results=Get-ADUser -Filter {SAMAccountName -like $id} -Properties * | select -property SAMAccountName,Name, PasswordNeverExpires,LockedOut,PasswordLastSet,(@{Expression={$_.PasswordLastSet.AddDays($maxPasswordAge)}}), LastLogOnDate,CanonicalName  | `
          ConvertTo-CSV -NoTypeInformation 
  
          }
  
      }
      End {
          $Headers
          $results
          }
  }
  
  Get-ADUserInfo -Users
  </code></pre>
  
  <p>Some notes:</p>
  
  <ol>
  <li>When calling Get-AdUserInfo -Users, the script needs to work by entering a single ID, *, or multiple IDs separated by a comma when using the -Users parameter.</li>
  <li>Using ConvertTo-CSV solved my biggest problem, comma separated output,thanks all.</li>
  <li>I'd like to get rid of the headers that are auto-created as well ("SAMAccountName","Name","PasswordNeverExpires","LockedOut","PasswordLastSet","$_.PasswordLastSet.AddDays($maxPasswordAge)","LastLogOnDate","CanonicalName"). How can I do that?  I've tried -skip 1 but that doesn't work with * and removes everything (including the data) if used with a single ID or IDs separated with commas.  I can't get -ExpandProperty to work either. Adding format-table -hidetableheaders at the end doesn't do anything as well</li>
  </ol>