title: Why is writing Excel cell values fast in VBScript but slow in PowerShell?
body: <p>Why is it that writing cell values to Excel is a lot faster in VBScript than in PowerShell?
  Isn't PowerShell the new thing, and VBScript the deprecated MS scripting language?</p>
  
  <p><strong>VBScript example (save to filename.vbs)</strong>
  This runs in a split second.</p>
  
  <pre><code>Set objExcel = CreateObject("Excel.Application")
  objExcel.Visible = false
  Set objWorkbook = objExcel.Workbooks.Add()
  
  ' Edit: increased number of writes to 500 to make speed difference more noticeable
  For row = 1 To 500
       'Edit: using .cells(row,1) instead of .cells(50,1) - this was a mistake
       objWorkbook.workSheets(1).cells(row,1).value = "test"
  Next
  
  objWorkbook.SaveAs(CreateObject("Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName) &amp; "\test.xlsx")
  objExcel.Quit
  msgbox "Done."
  </code></pre>
  
  <p><strong>PowerShell example (save to filename.ps1)</strong> This takes multiple seconds to run (problematic on thousands of records)</p>
  
  <pre><code>#need this to work around bug if you use a non-US locale: http://support.microsoft.com/default.aspx?scid=kb;en-us;320369
  [System.Threading.Thread]::CurrentThread.CurrentCulture = "en-US" 
  
  $excel = New-Object -ComObject Excel.Application
  $excel.Visible = $False
  $xls_workbook = $excel.Workbooks.Add()
  
  # Edit: using foreach instead of for
  # Edit: increased number of writes to 500 to make speed difference more noticeable
  foreach ($row in 1..500) {
      # Edit: Commented out print-line, slows down the script
      #"Row " + $row
      # This is very slow! - http://forums.redmondmag.com/forums/forum_posts.asp?tid=4037&amp;pn=7
      $xls_workbook.sheets.item(1).cells.item($row,1) = "test"
  }
  
  $xls_workbook.SaveAs($MyInvocation.MyCommand.Definition.Replace($MyInvocation.MyCommand.Name, "") + "test.xlsx")
  $excel.Quit()
  [System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel)
  </code></pre>
  
  <p>I want to use this for thousands of records. If there is no fast way to do this, PowerShell is not an option. Are there better alternatives?</p>