title: Export powershell script to CSV
body: <p>I have a small script which retrieves the LastLogonTimestamp and the SAMAccount for all users in a particular OU in AD and converts the timestamp to a date and extracts just the date from the string. That part works fine. I then would like to output that to a CSV so it may be opened in Excel and be perfectly formated into columns and look all pretty.</p>
  
  <p>I have tried ConvertTo-Csv and Export-Csv but have been uncuccessful. The problem is I am new to Powershell. This is my first script and I don't fully understand how this works. My script is probably terribly messy and illogical but it does the job so far.</p>
  
  <p>Please help. Thanks.</p>
  
  <pre><code>$userlist = Get-ADUser -SearchBase "OU=IT,DC=whatever,DC=com,DC=au" -Filter * -Properties * | Select-Object -Property Name,LastLogonTimestamp,SAMAccountName | Sort-Object -Property Name
  
  $userlist | ForEach-Object {
      $last = $_.LastLogonTimestamp;
      $ADName = $_.SAMAccountName;
  
      $tstamp = w32tm /ntte $last;
      if($tstamp.Length -lt "40"){}else
      {
          $ADDate = [DateTime]::Parse($tstamp.Split('-')[1]).ToString("dd/MM/yyyy")
          write-host $ADDate;
          write-host $ADName;
      }
  }
  </code></pre>