title: Powershell update SQL Server column
body: <p>Hi I am trying to update a column though doesnt seem to work. It extracts the data and does the date conversion in the string but the update section of my code below does not want to insert the changes Powershell makes.</p>
  
  <pre><code>## connect to db and do stuff 
  $Connection = new-object system.data.SqlClient.SQLConnection("Data Source=       (local);Integrated Security=SSPI;Initial Catalog=TESTDB");
  $sqlqry = "select row_id, DestFileName from FL_Input Where DestFileName like '%[0-9] '     + 'Jan%' + ' [0-9]%'" 
  $Command = new-object system.data.sqlclient.sqlcommand 
  $Command.CommandText = $sqlqry 
  $Command.Connection = $Connection 
  $Connection.Open()  
  
  $UpdateCmd = new-object System.Data.SqlClient.SqlCommand 
  $UpdateCmd.Connection = $Connection 
  
  ## for each filename, update timestamp - this replaces your $file in $files loop. 
  $Result = $Command.ExecuteReader() 
  while ($Result.Read()) { 
  
  $destfile = $Result['DestFileName'] 
  $srcfile = $destfile 
  $row_id = $Result['row_id'] 
  
  ## do all your regex stuff here 
  
  $destfile -match '\d{2}\s\w+\s\d{2,4}' | Out-Null &lt;# Test #&gt;  
  $destfile -match '\d{2}\-\w+\-\d{4}' | Out-Null &lt;# Test #&gt;  
  
  $destFile -replace "$(($matches).values)" , "$(get-date "$(($matches).Values)" -Format     yyyyMMdd)"   
  
  write-host "{0}-&gt;{1}" -f  $destfile, $srcfile
  
  # when you're finished doing your regex, push the result result back to the db: 
  $updateqry = "update FL_Input set DestFileName='{0}' WHERE Row_ID = {1} " -f $destfile, $row_id 
  $UpdateCmd.CommandText = $updateqry 
  $UpdateCmd.ExecuteNonQuery() 
  
  } 
  
  ## all done 
  $Connection.Close
  </code></pre>