title: Selecting data from a datatable in PowerShell
body: <p>I'm trying to figure out how to get the data i need from a Datatable. 
  I'm new to datatables and cannot figure out by reading the documentation how to properly build the needed query. </p>
  
  <p>I made a test script in PowerShell that i posted below:</p>
  
  <pre><code># clear the screen:
  CLS
  
  # Create the Datatable:
  $dt1 = New-Object System.Data.Datatable
  
  # Name the datatable:
  $dt1.TableName = "MyTable"
  
  # Add collumns to the table:
  [void]$dt1.Columns.Add("datacolumnone")
  [void]$dt1.Columns.Add("datacolumntwo")
  [void]$dt1.Columns.Add("datacolumnthree")
  
  # Add data:
  [void]$dt1.Rows.Add([string]"id 1", [string]"fact 1", [string]"purple")
  [void]$dt1.Rows.Add([string]"id 1", [string]"fact 2", [string]"black")
  [void]$dt1.Rows.Add([string]"id 1", [string]"fact 3", [string]"green")
  [void]$dt1.Rows.Add([string]"id 1", [string]"fact 4", [string]"green")
  [void]$dt1.Rows.Add([string]"id 2", [string]"fact 1", [string]"blue")
  [void]$dt1.Rows.Add([string]"id 2", [string]"fact 2", [string]"yellow")
  [void]$dt1.Rows.Add([string]"id 2", [string]"fact 3", [string]"purple")
  [void]$dt1.Rows.Add([string]"id 2", [string]"fact 4", [string]"yellow")
  [void]$dt1.Rows.Add([string]"id 3", [string]"fact 1", [string]"purple")
  [void]$dt1.Rows.Add([string]"id 3", [string]"fact 2", [string]"black")
  [void]$dt1.Rows.Add([string]"id 3", [string]"fact 3", [string]"green")
  [void]$dt1.Rows.Add([string]"id 3", [string]"fact 4", [string]"yellow")
  [void]$dt1.Rows.Add([string]"id 4", [string]"fact 1", [string]"purple")
  [void]$dt1.Rows.Add([string]"id 4", [string]"fact 2", [string]"yellow")
  [void]$dt1.Rows.Add([string]"id 4", [string]"fact 3", [string]"purple")
  [void]$dt1.Rows.Add([string]"id 4", [string]"fact 4", [string]"yellow")
  
  # Write the data to a xml and data file:
  $dt1.WriteXmlSchema('.\CacheSchema.xsd')
  $dt1.WriteXml(".\CacheData.xml")
  
  ##############################
  # End of this script         #
  # Going on with the next one #
  ##############################
  
  # Set the search criteria:
  $column1 = ""
  $column2 = "fact 1"
  $column3 = "purple"
  
  # build the query:
  $qquery1 = "
          datacolumnone like '%"+$column1+"%' 
      AND datacolumntwo like '%"+$column2+"%'
      AND datacolumnthree like '%"+$column3+"%'
  ";
  
  # Create the Datatable:
  $dt2 = New-Object System.Data.Datatable
  
  # Load the data:
  [void]$dt2.ReadXmlSchema(".\CacheSchema.xsd")
  [void]$dt2.ReadXml(".\CacheData.xml")
  
  # Run the query:
  $dt2.Select($qquery1)
  </code></pre>
  
  <p>Now what i try to achieve is:</p>
  
  <blockquote>
    <ol>
    <li>Find all id's (datacolumnone) from the rows that have the value: "fact 1" in "datacolumntwo" and "purple" in "datacolumnthree"</li>
    <li>Then further filter the id's where "datacolumnone" is in the results from the query above, "datacolumntwo" is "fact 4" and
    "datacolumnthree" is "yellow"</li>
    <li>Then when we have the filtered id's, show the rows that have in "datacolumnone" an id from the filtered query above and in
    "datacolumntwo" the value "fact 2"</li>
    </ol>
  </blockquote>
  
  <p><a href="https://msdn.microsoft.com/en-us/library/system.data.datatable.select%28v=vs.110%29.aspx" rel="nofollow">https://msdn.microsoft.com/en-us/library/system.data.datatable.select%28v=vs.110%29.aspx</a></p>