PowerShell Refresh Excel Power Query: How It Works

How-to basics · Updated November 20, 2026

Yes. On Windows, PowerShell can refresh Excel Power Query by automating the desktop Excel application through COM: open the workbook, call RefreshAll(), wait for supported asynchronous queries, and save it. This requires Excel to be installed; PowerShell alone does not execute Power Query inside an Excel file.

What PowerShell can and cannot refresh

Power Query is Excel’s tool for connecting to, transforming, and loading data. Its query definitions and refresh behavior are stored in the workbook, but the actual refresh is performed by an application or service that supports those connections. On a Windows computer with desktop Excel installed, PowerShell can ask Excel to open the workbook and refresh its queries.

The usual approach is Excel automation through COM, sometimes called Office automation. PowerShell controls an Excel instance much as a person would, using Excel’s object model. This is different from editing an .xlsx file directly. A module that reads or writes workbook cells without launching Excel generally cannot run Power Query transformations or refresh their connections.

COM automation is specific to Windows and the desktop Excel application. It is not a way to run Excel Power Query from macOS, Excel for the web, or a computer without Excel installed. If you need a cloud or server-based refresh, consider a service designed for that workload rather than assuming a local PowerShell script can substitute for Excel.

What happens during Refresh All

In Excel for Microsoft 365 on Windows, a person can refresh workbook data using Data > Refresh All. PowerShell can request the same broad workbook action with the Excel object model’s RefreshAll() method. Depending on the workbook, that may refresh Power Query connections and other external data connections, as well as related reports that are configured to refresh.

Excel may run some connections in the background. That means the call to refresh can return before every query has finished. A script that immediately saves or closes the workbook risks capturing old results or interrupting a refresh. Excel’s CalculateUntilAsyncQueriesDone() method can wait for pending asynchronous OLE DB and OLAP queries, but it is not a universal guarantee that every possible Power Query operation, source, or dependent task has completed successfully.

Refresh also does not necessarily mean that every query is loaded to a visible worksheet. A query can be configured as connection-only or loaded to the Data Model. The destination and load settings are part of the workbook’s design, so check the intended output rather than expecting every query to create a table on a sheet.

A basic PowerShell example

This Windows example opens an existing workbook in desktop Excel, requests a refresh, waits for supported asynchronous queries, saves the workbook, and closes Excel. Replace the sample path with a real workbook path. Run it in a user account that can access the file and its data sources.

$path = (Resolve-Path -LiteralPath 'C:\Data\Report.xlsx').Path
$excel = $null
$workbook = $null

try {
    $excel = New-Object -ComObject Excel.Application
    $excel.Visible = $false
    $excel.DisplayAlerts = $false

    $workbook = $excel.Workbooks.Open($path)
    $workbook.RefreshAll()
    $excel.CalculateUntilAsyncQueriesDone()
    $workbook.Save()
    $workbook.Close($false)
    [void][System.Runtime.InteropServices.Marshal]::ReleaseComObject($workbook)
    $workbook = $null
}
finally {
    if ($workbook -ne $null) {
        $workbook.Close($false)
        [void][System.Runtime.InteropServices.Marshal]::ReleaseComObject($workbook)
    }
    if ($excel -ne $null) {
        $excel.Quit()
        [void][System.Runtime.InteropServices.Marshal]::ReleaseComObject($excel)
    }
    [GC]::Collect()
    [GC]::WaitForPendingFinalizers()
}

The script uses try and finally so Excel is asked to close even if an error occurs. The workbook is saved only after the refresh-wait call returns. If an error occurs before that point, the cleanup closes the workbook without saving changes. In a production script, add logging and handle cleanup errors so that a close failure does not hide the original refresh error.

DisplayAlerts = $false suppresses some Excel alerts; it does not make every prompt disappear or resolve a missing credential. Avoid using it as a substitute for configuring the workbook and its data sources properly. Also note that a script can finish without proving that all returned data is correct: check refresh outcomes and the workbook’s expected output.

Preparing the workbook and data sources

Before automating a refresh, open the workbook in Excel interactively and confirm that the queries work there. This is a useful way to distinguish a PowerShell problem from a broken connection, expired sign-in, changed source location, or query error. Confirm that the workbook is not in a read-only location and that the account running the script can write to it.

  • Check each query’s source path, server, database, or web address, and make sure the source is reachable from the computer running the script.
  • Review the workbook’s saved data-source credentials and privacy settings. A refresh may need a sign-in or permission that is available to you interactively but not to a scheduled task.
  • Check whether queries load to a worksheet, the Data Model, or connection only, and identify which output should change after a successful refresh.
  • Make a backup before changing query settings or testing automation on an important workbook.

For a workbook you control, you can also review its connection and query properties in Excel. Background refresh settings affect when a refresh returns control to Excel. Disabling background refresh may be useful for particular supported connections, but connection types expose different settings; do not assume one setting applies to every Power Query source.

Waiting, checking completion, and saving

There is no single refresh-wait technique that is guaranteed to cover every workbook configuration. RefreshAll() starts the requested work, while CalculateUntilAsyncQueriesDone() waits for pending asynchronous OLE DB and OLAP queries. Those methods are often useful together, but a query can fail, require credentials, or behave differently depending on its connection and Excel version.

For a dependable process, test the exact workbook and sources under the same Windows account that will run the job. Check that refreshed values or a known refresh timestamp changed as expected, and log the time, workbook path, and any errors. If a query is slow, establish a reasonable job timeout and investigate the source rather than saving after an arbitrary short delay. A fixed sleep can either wait too little or waste time; it is not evidence that refresh succeeded.

Saving is a separate decision from refreshing. The example saves the workbook after the wait call, but you may want to save only after verifying the expected result. If the workbook contains formulas, PivotTables, or downstream reports, confirm whether those also need recalculation or refresh for your use case. Refreshing source queries does not guarantee every dependent calculation has produced the business result you expect.

Running it on a schedule

PowerShell scripts can be launched by Windows Task Scheduler, but a scheduled run is more demanding than running the script at a logged-in desktop. The task must use an account with access to the workbook, network locations, and data sources. Mapped drives, interactive sign-in prompts, and stored credentials may not be available in the task’s execution context. A UNC path and appropriately granted permissions are often easier to reason about than a drive letter mapped in a user session.

Excel desktop automation is not a robust general-purpose server service. Microsoft cautions against unattended, non-interactive Office automation in server-side scenarios because Office applications can display dialogs, depend on a user profile, or hang in ways that are difficult to recover from. If the refresh is business-critical or must run as a reliable unattended service, evaluate a supported data-refresh platform or redesign the process for that environment.

When testing a scheduled task, begin with a copy of the workbook and capture standard output, errors, exit status, and timestamps in a log. Verify the task’s working directory and permissions. Avoid launching multiple refresh jobs against the same file at once; concurrent Excel instances can contend for the workbook or produce confusing results.

Common problems and practical fixes

  • Excel remains in Task Manager: A COM reference may still be alive, or Excel may be waiting on a dialog or connection. Close workbook and application objects in cleanup, release references, and inspect the task’s run context. Do not blindly terminate every Excel process, since another user may have an open workbook.
  • The script ends but data looks unchanged: The query may have failed, the source may contain the same data, the result may load somewhere unexpected, or background work may not have finished. Check Excel’s connection errors and the intended output rather than treating a normal script exit as proof of success.
  • A credentials prompt blocks the run: Sign-in state and permissions can differ between an interactive user and a scheduled account. Resolve access through the organization’s approved authentication and credential process; do not put passwords or tokens directly into a script.
  • The file cannot be opened or saved: Check the path, file permissions, read-only status, file locks, and whether another Excel session has it open. A network share can also be unavailable under the scheduled account.
  • The refresh works manually but not unattended: The interactive session may provide a profile, authentication token, mapped drive, or prompt response that the task does not have. Reproduce the task’s account and environment when diagnosing it.

PowerShell automation compared with other options

For an occasional refresh, Excel’s Data > Refresh All command is simplest. It lets a person see prompts and errors directly. Excel can also be configured to refresh connections when a workbook opens, where that option is available for the connection. That setting is convenient for a user opening the file, but it does not by itself provide reliable scheduling, monitoring, or a way to verify a successful unattended run.

PowerShell with COM is appropriate when a Windows desktop workflow needs to open and save an Excel workbook using installed Excel. It offers flexibility, but inherits Excel’s dependency on a desktop application and its connection behavior. A package such as ImportExcel can be useful for reading or editing workbook content without Excel, but it does not run Excel’s Power Query engine simply because it can manipulate an .xlsx file.

For centrally managed refreshes, investigate Microsoft’s cloud data and automation offerings that match the source and destination, such as Power BI or Power Automate where the particular connector and licensing support the scenario. Their capabilities and requirements vary; they are not interchangeable with every Excel query. Excel for the web also has different capabilities from desktop Excel, so check whether the workbook’s specific connections support refresh there before relying on it.

Frequently Asked Questions

Can PowerShell refresh Power Query without Excel installed?
No, not with the desktop COM method described here. It automates the Excel application, so Excel must be installed on a supported Windows computer. PowerShell by itself does not provide Excel’s Power Query refresh engine.

Does RefreshAll wait until every query is finished?
Not necessarily. Some refreshes run asynchronously. CalculateUntilAsyncQueriesDone() waits for pending asynchronous OLE DB and OLAP queries, but it should not be treated as a universal completion check for every connection or as proof that refresh succeeded.

Can I run this from PowerShell 7?
PowerShell 7 on Windows can create COM objects, but the automation still depends on installed desktop Excel and the Windows user environment. Test the specific script, Excel installation, and execution context; using a different PowerShell version does not remove Excel’s unattended-automation limitations.

Can I refresh an Excel workbook on a Mac with this script?
No. The example uses Windows COM automation, which is not the Mac Excel automation interface. Excel for Mac has different automation options, and this Windows PowerShell script is not portable to macOS.

Will ImportExcel refresh my queries?
Generally, no. ImportExcel can create and manipulate workbook files without launching desktop Excel, but it does not execute the Power Query engine. Use Excel automation or a refresh service that explicitly supports the workbook’s connections.

Bottom line

PowerShell can request an Excel Power Query refresh on Windows by controlling desktop Excel, typically with RefreshAll(), a suitable wait, and a deliberate save. The key is not just starting the refresh: confirm source access, account permissions, completion behavior, and refreshed output. For scheduled or mission-critical refreshes, test the exact workbook in its real execution context and consider a service designed for unattended data workflows.

Related

Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.

More in How-to basics

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.