title: How to create a new sheet in excel with powershell script
body: <p>My question is in the title, i dont know how to create a new sheet in excel using a powershell script.</p>
  <p>here is my code :</p>
  <pre><code>#Define locations and delimiter
  $csv = $pathcsv
  $xlsx = $pathxlsx
  $delimiter = &quot;,&quot; #Specify the delimiter used in the file
  
  # Create a new Excel workbook with one empty sheet
  $excel = New-Object -ComObject excel.application 
  $workbook = $excel.Workbooks.Add(1)
  $worksheet = $workbook.worksheets.Item(1)
  
  # Build the QueryTables.Add command and reformat the data
  $TxtConnector = (&quot;TEXT;&quot; + $csv)
  $Connector = $worksheet.QueryTables.add($TxtConnector,$worksheet.Range(&quot;A1&quot;))
  $query = $worksheet.QueryTables.item($Connector.name)
  $query.TextFileOtherDelimiter = $delimiter
  $query.TextFileParseType  = 1
  $query.TextFileColumnDataTypes = ,1 * $worksheet.Cells.Columns.Count
  $query.AdjustColumnWidth = 1
  
  # Execute &amp; delete the import query
  $query.Refresh()
  $query.Delete()
  
  # Save &amp; close the Workbook as XLSX.
  $Workbook.SaveAs($xlsx,51)
  
  $Workbook = $excel.Workbooks.Open($pathxlsx)
  
  Add-Worksheet -ExcelPackage $excel -WorkSheetname &quot;test&quot;
  
  $excel.Quit()
  pause
  </code></pre>
  <p>So i try the commande Add-Worksheet -ExcelPackage $excel -WorkSheetname &quot;test&quot; but it seems not working, can you help me with that ?</p>
  <p>I got the following error :</p>
  <p>Add-Worksheet : Unable to process argument transformation on parameter &quot;ExcelPackage&quot;. impossible to
  convert the value &quot;Microsoft.Office.Interop.Excel.ApplicationClass&quot; to type &quot;
  Microsoft.Office.Interop.Excel.ApplicationClass &quot;and type&quot; OfficeOpenXml.ExcelPackage &quot;.
  Au caractère C:....\NEW.ps1:125 : 29</p>
  <ul>
  <li>Add-Worksheet -ExcelPackage $excel -WorkSheetname &quot;NewSheet&quot;</li>
  <li>
  <pre><code>                        ~~~~~~
  </code></pre>
  <ul>
  <li>CategoryInfo          : InvalidData : (:) [Add-Worksheet], ParameterBindingArgumentTransformationException</li>
  <li>FullyQualifiedErrorId : ParameterArgumentTransformationError,Add-Worksheet</li>
  </ul>
  </li>
  </ul>
  <p>thanks</p>