title: How to create a table if not present using powershell
body: <p>i new to scripting and want to create SQL table if not present , and update its content. But when i try to run it throws an exception.
  <strong>Error Message</strong>:  Exception calling &quot;ExecuteReader&quot; with &quot;0&quot; argument(s): &quot;Invalid object name 'test.dbo.testStatus'.&quot;</p>
  <p>I can't use Invoke Command for SQL query or any module based installation for SQL operation.</p>
  <p>my code:</p>
  <pre><code>try{
          #SQL Query
          $CheckQuery='SELECT COUNT(1) FROM [test].[dbo].[testStatus]'
          $InsertQuery=&quot;INSERT INTO [test].[dbo].[testStatus]
                     ([DATE],[HOST_NAME],[FOLDER_NAME],[FOLDER_PATH],[STATUS])
               VALUES('$date','$ComputerName','$name','$path','$status')
          &quot;
          $CreateQuery='CREATE TABLE testStatus (DATE DATE,HOST_NAME NVARCHAR(MAX),FOLDER_NAME NVARCHAR(MAX),FOLDER_PATH NVARCHAR(MAX),STATUS NVARCHAR(MAX))'
          
          
          $connString = &quot;Data Source=$SqlServer;Database=$Database;User ID=$SqlAuthLogin;Password=$SqlAuthPw&quot;
          
          #Create a SQL connection object
          $conn = New-Object System.Data.SqlClient.SqlConnection $connString
          
          #Attempt to open the connection
          $conn.Open()
          if($conn.State -eq &quot;Open&quot;)
          {
          Write-Host &quot;Test connection successfull&quot;
          &quot;$(get-date -format &quot;yyyy-MM-dd HH:mm:ss&quot;):Connection Established&quot; | out-file $LogFile -Append
          $SqlCmd = New-Object System.Data.SqlClient.SqlCommand  
          $SqlCmd.CommandText = $CheckQuery  
          $SqlCmd.Connection = $conn  
          $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter  
          $SqlAdapter.SelectCommand = $SqlCmd   
          #Creating Dataset  
          $Datatable = New-Object &quot;System.Data.Datatable&quot;
          $result = $SqlCmd.ExecuteReader()
          $Datatable.Load($result)
          $TableExistCheck=$Datatable
          if($TableExistCheck)
          {
          
          #Insert into Table
          $SqlCmd.CommandText = $InsertQuery  
          $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter  
          $SqlAdapter.SelectCommand = $SqlCmd   
          #Creating Dataset  
          $Datatable = New-Object &quot;System.Data.Datatable&quot;
          $result = $SqlCmd.ExecuteReader()
          $conn.Close()
          &quot;$(get-date -format &quot;yyyy-MM-dd HH:mm:ss&quot;):Successfully Inserted Data&quot; | out-file $LogFile -Append
          }
          else{
          #Create Table
          $SqlCmd.CommandText = $CreateQuery  
          $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter  
          $SqlAdapter.SelectCommand = $SqlCmd  
          #Creating Dataset  
          $Datatable = New-Object &quot;System.Data.Datatable&quot;
          $result = $SqlCmd.ExecuteReader()
          
          #Insert into Table
          $SqlCmd.CommandText = $InsertQuery  
          $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter  
          $SqlAdapter.SelectCommand = $SqlCmd   
          #Creating Dataset  
          $Datatable = New-Object &quot;System.Data.Datatable&quot;
          $result = $SqlCmd.ExecuteReader()
          $conn.Close()
          &quot;$(get-date -format &quot;yyyy-MM-dd HH:mm:ss&quot;):Successfully Created Table and Inserted Data&quot; | out-file $LogFile -Append
          }
          }  
          }
          catch
          {
             Write-Host &quot;`nError Message: &quot; $_.Exception.Message
             Write-Host &quot;`nError in Line: &quot; $_.InvocationInfo.Line
             Write-Host &quot;`nError in Line Number: &quot;$_.InvocationInfo.ScriptLineNumber
             Write-Host &quot;`nError Item Name: &quot;$_.Exception.ItemName
             #Set-Content -Path $LogFile -Value $_.Exception.Message
             #LogMessage -Message &quot;Error:&quot; $_.Exception.Message;
             &quot;$(get-date -format &quot;yyyy-MM-dd HH:mm:ss&quot;):$($_.Exception.Message)&quot; | out-file $LogFile -Append
          }
  </code></pre>
  <p>What Changes should I make in above script , to create a table if not exist and insert data into it.</p>
  <p>Any help will be thankfull.</p>