title: Powershell Script - Open Excel, Update External Data, Save as
body: <p>Is anyone able to help me with this script. </p>
  
  <pre><code>$file = 'C:\Scripts\Spreadsheet.xlsx'
  $x1 = New-Object -ComObject "Excel.Application"
  $x1.Visible = $false
  $enddate = (Get-Date).tostring("dd-MM-yy")
  $filename = 'C:\Scripts\Spreadsheet ' + $enddate + '.xlsx'
  $wb = $x1.workbooks.Open($file)
  $wb.refreshall
  $wb.SaveAs($filename)
  $wb.Close()
  $x1.Quit()
  Remove-Variable wb,x1
  </code></pre>
  
  <p>The workbook opens and updates its pivot table with external data through an ODBC connection. When you open the workbook manually it refreshes. When you open it with the script it just opens and does not refresh the data.</p>
  
  <p>I have tried the following:</p>
  
  <ul>
  <li>Checking the checkbox <em>"Always use connection file"</em></li>
  <li>Saving the password for the data source inside excel</li>
  <li>Disabling <em>"Enable background refresh"</em> and all the other refresh options</li>
  <li>Created a macro to automatically refresh the data source when the workbook opens</li>
  </ul>
  
  <p>Any help would be appreciated, thanks!</p>