bash 的 exec() 的 PowerShell 等价物是什么?

What is the PowerShell equivalent of bash's exec()?

这里是 PowerShell 的新手。在不创建子 shell 的情况下从当前进程中 运行 新脚本的最佳方法是什么?在 Bash 中,您可以通过以下方式执行此操作:

source script # Executes the commands in script within the current shell

exec script # Same as source, except it completely replaces the current process with script

PowerShell 中是否有相应的等效项?

您也可以使用.\Script.ps1

或开始输入名称并使用 TAB 完成脚本名称。

点源脚本允许函数在命令行上可用。

例如:

Function Get-LocalTime { 

$CurTime = Get-Date

"The Date and time currently is $CurTime"

}

现在我们点源代码(将上面的脚本命名为Example.ps1)PS C:\>. .\Example.ps1

允许我们简单地键入和 Tab 完成 Get-LocalTime

-编辑评论,您可以定义一个函数并立即在同一个脚本中使用该函数。所以在 Example.ps1 中,在最后一行输入 Get-LocalTime