title: How to retrieve OUTPUT statement when calling stored proc through Powershell
body: <p>I am running the following script in powershell, however I don't seem to be able to retrieve any PRINT statements or error messages? How do I capture all outputs within the powershell session please?</p>
  
  <pre><code>$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
  $SqlConnection.ConnectionString = "Server=$sql_server;Database=$sql_db;user ID=$sql_usr;password=$sql_pwd"
  $SqlCmd = New-Object System.Data.SqlClient.SqlCommand
  $SqlCmd.Connection = $SqlConnection
  $SqlCmd.CommandText = "$storedProc"
  $SqlCmd.CommandType = [System.Data.CommandType]::StoredProcedure
  $SqlCmd.Parameters.Add("@COBDate", "$dateVariable")
  $handler = [System.Data.SqlClient.SqlInfoMessageEventHandler] {param($sender, $event) Write-Host $event.Message }; 
  $SqlConnection.add_InfoMessage($handler); 
  $SqlConnection.FireInfoMessageEventOnUserErrors = $true;
  $SqlConnection.Open()
  $SqlCmd.ExecuteNonQuery()
  $SqlCmd.Parameters.value
  $SqlConnection.Close()
  </code></pre>