如何让 Powershell 等到命令完成后再继续?

How do I make Powershell wait until a command is done before continuing?

我的脚本在安装较新版本之前卸载了 Windows 应用商店应用。我需要确保卸载完成后再安装,那么如何确定我已经等了足够长的时间?

Remove-Appxpackage MyAppName  
# ~wait here~  
Add-Appxpackage .\PathToNewVersion

您可以使用 Start-Job and Wait-Job cmdlet 执行此操作:

Start-Job -Name Job1 -ScriptBlock { Remove-Appxpackage MyAppName }
Wait-Job -Name Job1
Add-Appxpackage .\PathToNewVersion

Start-Job 将启动卸载应用程序的新作业进程。 Wait-Job 将导致脚本等待任务完成后再继续。