title: Import-CSV Powershell v2.0 versus v3.0
body: <p>I have a script that needs to run both on Powershell v2.0 and v3.0
  It imports a csv file and reads a particular value from the first row only. The csv file can have a single row of values or multiple.</p>
  
  <pre><code>$file_data = Import-csv -Path $file_name
  
  $col5 = "ColumnName"
  </code></pre>
  
  <p>...</p>
  
  <p>then use <code>$file_data[0].$col5</code> in some statement</p>
  
  <p>Here's the difference in <code>Import-csv</code>'s behavior between Powershell v2.0 and v3.0</p>
  
  <ol>
  <li><p>On Powershell v2.0, Import-csv returns a non array PSObject object when there is a single row in csv file. So, the above line will throw an error "CannotIndex" <code>$file_data</code>.
  But when you have multiple rows in csv, Powershell v2.0 will work fine.</p></li>
  <li><p>On Powershell v3.0, Import-csv always returns an array, so indexing is not a problem.</p></li>
  </ol>
  
  <p>But if I remove the index and use just $file_data.$col5, Powershell v2.0 will not show any data when the csv has multiple rows.</p>
  
  <p>I am missing out on some thing here. There should be an elagant way to deal with it.</p>