I'm trying to run

Get-AppXPackage -AllUsers | Foreach {Add-AppxPackage -DisableDevelopmentMode -Register "$($_.InstallLocation)\AppXManifest.xml"}

from Power Shell as administrator in order to restore broken Search and Cortana functionality. But at some point it just fails with

Add-AppxPackage : Deployment failed with HRESULT: 0x80073D02, The package could not be installed because resources it modifies are currently in use.
error 0x80004004: Unable to install because the following apps need to be closed

What is the proper way to run Get-AppXPackag to avoid (or work around) such conflict?