title: How to get stored procedure output parameter into variable using Powershell?
body: <p>I have a stored procedure returning a string and I need the result as a powershell variable. I'm stuck on the output parameter syntax.</p>
  
  <p>Powershell script:</p>
  
  <pre><code>$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
  $SqlConnection.ConnectionString = "Server=myserver;Database=mydb;Integrated Security=True"
  $SqlConnection.Open()
  $SqlConnection.State
  
  $SqlCmd = New-Object System.Data.SqlClient.SqlCommand
  $SqlCmd.CommandText = "testsp3"
  $SqlCmd.Connection = $SqlConnection
  
  
  $op = new-object System.Data.SqlClient.SqlParameter;
  $op.ParameterName = "@answer";
  $op.Direction = [System.Data.ParameterDirection]'Output';
  $op.DbType = [System.Data.DbType]'String';
  $op.Size = 2500
  
  $SqlCmd.Parameters.Add($op);
  
  $what = $SqlCmd.ExecuteScalar();
  
  $what
  </code></pre>
  
  <p>Error message:</p>
  
  <blockquote>
    <p>Exception calling "ExecuteScalar" with "0" argument(s): "Procedure or function 'testsp3' expects parameter '@answer', which was not supplied."</p>
  </blockquote>
  
  <p>Stored procedure if it matters:</p>
  
  <pre><code>CREATE PROCEDURE [dbo].[testsp3]
  @answer nvarchar(max) output
  
  AS
  BEGIN
      SET NOCOUNT ON;
  
      SELECT @answer = 'blah blah blah'
      RETURN
  END
  GO
  </code></pre>