title: Retrieving data using select SQL statement in Powershell
body: <p>My goal is to assign the value of the results returned to a variable:   </p>
  
  <pre><code>$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
  $SqlConnection.ConnectionString = "Server=HOME\SQLEXPRESS;Database=master;Integrated Security=True"
  $SqlCmd = New-Object System.Data.SqlClient.SqlCommand
  $SqlCmd.CommandText = "select name from sysdatabases where name = 'tempdb'"
  $SqlCmd.Connection = $SqlConnection
  $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
  $SqlAdapter.SelectCommand = $SqlCmd
  $DataSet = New-Object System.Data.DataSet
  $SqlAdapter.Fill($DataSet)
  $SqlConnection.Close()
  $DataSet.Tables[0]
  </code></pre>
  
  <p>The value returned should obviously be 'tempdb', so how can I assign this to a variable so this will work:</p>
  
  <pre><code>Write-output "Database is " $variablename
  </code></pre>
  
  <p>Desired output: Database is tempdb</p>